Java Utililty Methods Time Format

List of utility methods to do Time Format

Description

The list of methods to do Time Format are organized into topic(s).

Method

StringgetFormatDatetime()
get Format Datetime
GregorianCalendar gCalendar = new GregorianCalendar();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDateTime;
try {
    strDateTime = formatter.format(gCalendar.getTime());
} catch (Exception ex) {
    System.out.println("Error Message:".concat(String.valueOf(String.valueOf(ex.toString()))));
    String s = null;
...
StringgetFormatDateTime(Date date)
get Format Date Time
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
StringgetFormatDateTime(Date date, String format)
get Format Date Time
SimpleDateFormat sdf;
if (format == null || format.equalsIgnoreCase("")) {
    sdf = new SimpleDateFormat(DEFAULT_FORMAT);
} else {
    sdf = new SimpleDateFormat(format);
return sdf.format(date);
StringgetFormatDateTime(java.util.Date date)
get Format Date Time
return format(date, "yyyyMMddHHmmss");
StringgetFormattedCurrentDateAndTime()
get Formatted Current Date And Time
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
return formatter.format(date);
StringgetFormattedCurrentDateTime(String pattern)
This method returns a Date initialized without milliseconds.
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
String dateString = formatter.format(date);
return dateString;
StringgetFormattedDateAndTime(final Date date)
get Formatted Date And Time
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
return dateFormat.format(date);
StringgetFormattedDateTime(Date date, String pattern)
get Formatted Date Time
SimpleDateFormat sdfDate = new SimpleDateFormat(pattern);
return sdfDate.format(date);
StringgetFormattedDateTime(String date, String dateFormat)
get Formatted Date Time
try {
    return new SimpleDateFormat(dateFormat)
            .format(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(date));
} catch (ParseException e) {
    return "";
StringgetFormattedDuration(long fromTime)
get Formatted Duration
long milliseconds = getDuration(fromTime);
long seconds = milliseconds / MS_IN_SECOND;
long minutes = seconds / SEC_IN_MINUTE;
Object[] args = { Long.valueOf(minutes), Long.valueOf(seconds % SEC_IN_MINUTE) };
return TIME_FORMAT.format(args);