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

longgetTime(String dateStr, String formate)
get Time
Date date;
try {
    date = getDate(dateStr, formate);
    return date.getTime();
} catch (ParseException e) {
    return -1;
StringgetTime(String format)
get Time
return formatDate(new Date(), format);
longgetTime(String format, String date)
get Time
if (format == null || "".equals(format))
    format = "yyyy-MM-dd";
SimpleDateFormat simpledateformat = new SimpleDateFormat(format, Locale.SIMPLIFIED_CHINESE);
Date d = null;
try {
    d = (Date) simpledateformat.parse(date);
} catch (ParseException e) {
    e.printStackTrace();
...
StringgetTime(String timeFormat)
Gets the time at the instant of invocation.
return new SimpleDateFormat(timeFormat).format(Calendar.getInstance().getTime());
StringgetTimeAfter(int field, int amount, String formatStr)
get Time After
Calendar cal = Calendar.getInstance();
cal.add(field, amount);
SimpleDateFormat sp = new SimpleDateFormat(formatStr);
return sp.format(cal.getTime());
StringgetTimeAsString(final Date date, final String format)
Returns the given Date as a formatted String for the given formatting rule.
SimpleDateFormat frmt = new SimpleDateFormat(format);
return frmt.format(date);
StringgetTimeAsString(String timeFormatAsString)
get Time As String
DateFormat timeFormat = new SimpleDateFormat(timeFormatAsString);
Calendar calendar = Calendar.getInstance();
return timeFormat.format(calendar.getTime());
StringgetTimeByDate(Date date, String timeFormat)
get Time By Date
if (date == null)
    return null;
if (timeFormat == null) {
    timeFormat = "MM/dd/yyy hh:mm:ss";
return new SimpleDateFormat(timeFormat).format(date);
StringgetTimeByFormat(Date date, String format)
Gets the time by format.
return format(date, format, Locale.ENGLISH);
longgetTimeCount(String from, String to, String format)
get Time Count
Date d1 = dateFormatCheck(from, format);
Date d2 = dateFormatCheck(to, format);
long duration = d2.getTime() - d1.getTime();
return duration;