Java Utililty Methods Date to String

List of utility methods to do Date to String

Description

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

Method

StringdateToISODate(Date date)
date To ISO Date
TimeZone tz = TimeZone.getTimeZone("UTC");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
dateFormat.setTimeZone(tz);
return dateFormat.format(date);
StringdateToOracleDateString(Date date)
date To Oracle Date String
System.out.println("To Oracle Date; [" + formatDate(date, "dd-MMM-yy") + "]");
return formatDate(date, "dd-MMM-yy");
StringdateToRelativeTime(Date date)
date To Relative Time
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
long between = (now.getTime() - date.getTime()) / 1000;
long day = between / (24 * 3600);
long hour = between % (24 * 3600) / 3600;
long minute = between % 3600 / 60;
long second = between;
String timeStr;
...
StringdateToRFC1123(Date date)
date To RFC
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, new Locale("en"));
dateFormat.setTimeZone(TimeZone.getTimeZone(GMT));
String result = dateFormat.format(date);
return result;
StringdateToRFC3339(Date d)
Format dates as specified in rfc3339 (required for Atom dates)
StringBuilder result = new StringBuilder(rfc3339.format(d));
Calendar cal = new GregorianCalendar();
cal.setTime(d);
cal.setTimeZone(TimeZone.getDefault());
int offset_millis = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
int offset_hours = Math.abs(offset_millis / (1000 * 60 * 60));
int offset_minutes = Math.abs((offset_millis / (1000 * 60)) % 60);
if (offset_millis == 0) {
...
StringdateToStr(Date date)
date To Str
String s = "";
if (date == null) {
    return "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
    s = sdf.format(date);
} catch (Exception e) {
...
StringdateToStr(Date date)
date To Str
if (date == null) {
    return "";
return dateToString(date, NORMAL_PATTERN);
StringdateToStr(Date date)
date To Str
return dateToStr(date, "yyyy/MM/dd");
StringdateToStr(Date date)
date To Str
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(date).trim();
StringdateToStr(Date date, int type)
date To Str
switch (type) {
case 0:
    return dateToStr(date);
case 1:
    return dateToStr(date, "yyyy/MM");
case 2:
    return dateToStr(date, "yyyyMMdd");
case 11:
...