Java Utililty Methods String to Time

List of utility methods to do String to Time

Description

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

Method

DategetAsDateTime(String dateTime)
get As Date Time
try {
    return new SimpleDateFormat(FORMAT).parse(dateTime);
} catch (ParseException e) {
    throw new IllegalArgumentException(e);
CalendargetCalendarForSpecifiedDate(final String dateTime)
Gets the calendar for specified date.
final Calendar calTime = Calendar.getInstance();
final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FMT_FULL, Locale.ENGLISH);
final Date date = (Date) sdf.parse(dateTime);
calTime.setTime(date);
return calTime;
CalendargetCalendarFromCPEGeneratorDateTime(String cpeDateTime)
Take a value of a cpe dictionary's generator timestamp and return a Calendar object.
Calendar c = null;
String dateFormatString = "yyyy-MM-dd'T'hh:mm:ss";
try {
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormatString);
    Date d = sdf.parse(cpeDateTime);
    c = Calendar.getInstance();
    c.setTime(d);
} catch (Exception e) {
...
CalendargetCalendarFromDate(final String dateTime)

getCalendarFromDate.

final Calendar cal = GregorianCalendar.getInstance();
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
    final Date date = sdf.parse(dateTime);
    cal.setTime(date);
} catch (final ParseException e) {
    return null;
return cal;
StringgetDlayDateTime(String playDate, String playTime)
get Dlay Date Time
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("HH:mm:ss");
StringBuffer sb = new StringBuffer();
try {
    playDate = format1.format(format.parse(playDate));
} catch (ParseException e) {
    e.printStackTrace();
...
StringgetEndTime(String flag, int value)
get End Time
Calendar calendar = Calendar.getInstance();
if (flag.equals("DAY")) {
    calendar.add(Calendar.DAY_OF_MONTH, -value);
} else if (flag.equals("HOUR")) {
    calendar.add(Calendar.HOUR_OF_DAY, -value);
} else if (flag.equals("MINUTE")) {
    calendar.add(Calendar.MINUTE, -value);
Date dateTime = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dateTime);
DategetExecutionDateTime(String input)
get Execution Date Time
return executionFormat.parse(input);
DategetExpireTime(String timeStr)
get Expire Time
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
Date date = formatter.parse(timeStr, new ParsePosition(0));
return date;
StringgetFormerDate(String dateTime)
get Former Date
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
long dif = df.parse(dateTime).getTime() - 86400 * 1000;
Date date = new Date();
date.setTime(dif);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
StringgetIdByTime(String name)
get Id By Time
return name + "-" + getCurrDate() + "-" + getCurrTime() + "-" + getRandomNumber();