Skip to content

Instantly share code, notes, and snippets.

@vigneshrcsengg
Created September 26, 2021 10:03
Show Gist options
  • Save vigneshrcsengg/a61bde3e03d770c34959c79fed7ed9ee to your computer and use it in GitHub Desktop.
Save vigneshrcsengg/a61bde3e03d770c34959c79fed7ed9ee to your computer and use it in GitHub Desktop.
Date Utilities
import java.text.SimpleDateFormat;
import java.time.Month;
import java.time.format.TextStyle;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
class DateUtil {
public static void main(String args[]) {
}
public static int getCurrentYear() {
return Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date()));
}
public static int getNextYear() {
return getCurrentYear() + 1;
}
public static int getNextMonthYear(int month) {
return month == 12 ? getCurrentYear() + 1 : getCurrentYear();
}
public static int getCurrentMonth() {
return Integer.parseInt(new SimpleDateFormat("MM").format(new Date()));
}
public static int getNextMonth() {
return getCurrentMonth() == 12 ? 1 : getCurrentMonth() + 1;
}
public static int getMonth(String monthName) {
return Month.valueOf(monthName.toUpperCase()).getValue();
}
public static String getMonthLocaleName(int month) {
return Month.of(month).getDisplayName(TextStyle.FULL, Locale.ENGLISH).toUpperCase();
}
public static String getMonthLocaleShortName(int month) {
return Month.of(month).getDisplayName(TextStyle.SHORT, Locale.ENGLISH).toUpperCase();
}
public static String getTodayDate() {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy");
return formatter.format(date);
}
public static String getTodayDate(String format) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.format(date);
}
public static String getDateFromNow(int months, String format) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, months);
Date nextYear = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.format(nextYear);
}
public static String getTimeNow() {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
return formatter.format(date);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment