In this post I will share some frequently use methods for Date/Time processing. These methods are needed for almost every project or project starters. Feel free to use and add more features to my class.
First are some Date Time Formatter like date formatter, 24h time formatter, 24h date time formatter…
public static final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
public static final SimpleDateFormat monthYearFormater = new SimpleDateFormat("MM/yyyy");
public static final SimpleDateFormat timeFormatter = new SimpleDateFormat("hh:mm:ss");
public static final SimpleDateFormat datetimeFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
public static final SimpleDateFormat datetimeFormatterNoSecond = new SimpleDateFormat("dd/MM/yyyy HH:mm");
public static final SimpleDateFormat hourMinuteFormater = new SimpleDateFormat("HH:mm");
public static final SimpleDateFormat timeStampFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
You will use these formatters to parse String into Date so that you can use for calculation. Or you can use these formatters to format a Date into String so that you can use it to print out a console or notify user via a message.
Next is methods needed to get a Calendar instance, to operate with Date object, you definitely will need Calendar object.
public static Calendar getCalendar(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
public static Calendar getCalendar() {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
return c;
}
Now what if you know date/month/year and want to have a Date object? The following methods will help you to build Date
/**
* Build Date from date/month/year. Zero-based for Month (0=January).
*
* @return
*/
public static Date buildDate(int year, int month, int date) {
Calendar calendar = getCalendar();
calendar.set(year, month, date);
return calendar.getTime();
}
/**
* Build Datetime from date/month/year/hour/minute/second. Zero-based for
* Month (0=January).
*
* @param year
* @param month
* @param date
* @param hour
* @param minute
* @param second
* @return
*/
public static Date buildDateTime(int year, int month, int date, int hour, int minute, int second) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, date, hour, minute, second);
return calendar.getTime();
}
Sometime you will also need to remove the Time part from a Datetime object, here is the method for that purpose
/**
* Delete time from Date.
*
* @param date
* @return
*/
public static Date removeTime(Date date) {
if (date == null) {
return null;
}
// Obtain an instance of the Calendar.
Calendar calendar = getCalendar(date);
// Mark no automatic correction
calendar.setLenient(false);
// Remove the hours, minutes, seconds and milliseconds.
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// Return the date again.
return calendar.getTime();
}
And not only to build a new Date object, you can use the following methods to adjust date, like get the previous date or get the next date or get yesterday or get tomorrow date…