Java Utililty Methods Date Convert

List of utility methods to do Date Convert

Description

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

Method

StringdateToString(final Date date)
date To String
final StringBuilder res = new StringBuilder();
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
res.append(cal.get(Calendar.YEAR)).append("-");
final int month = cal.get(Calendar.MONTH) + 1;
res.append((month < 10) ? "0" + month : month).append("-");
final int day = cal.get(Calendar.DAY_OF_MONTH);
res.append((day < 10) ? "0" + day : day).append(" ");
...
longdateToUnixTimestamp(Date date)
date To Unix Timestamp
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getTimeInMillis() / 1000;
StringdateToUTC(Date d)
Converts a Date object into a string in 'Zulu' format
StringBuffer date = new StringBuffer();
Calendar cal = Calendar.getInstance();
cal.setTime(d);
date.append(cal.get(Calendar.YEAR));
date.append(printTwoDigits(cal.get(Calendar.MONTH) + 1)).append(printTwoDigits(cal.get(Calendar.DATE)))
        .append("T");
date.append(printTwoDigits(cal.get(Calendar.HOUR_OF_DAY))).append(printTwoDigits(cal.get(Calendar.MINUTE)))
        .append(printTwoDigits(cal.get(Calendar.SECOND))).append("Z");
...
StringdateToXdsDateString(Date date, int format)
date To Xds Date String
TimeZone tz = TimeZone.getTimeZone("GMT");
Calendar cal = new GregorianCalendar(tz);
cal.setTime(date);
return getXdsDate(cal, format);
StringdateToXmlDateTime(final Date date)
Calculates a valid xsd:dateTime value from a given date.
final TimeZone utc = TimeZone.getTimeZone("GMT");
final Calendar c = Calendar.getInstance(utc);
c.setTime(date);
final StringBuffer result = new StringBuffer(24);
result.append(c.get(Calendar.YEAR)).append('-');
appendTwoDigits(result, c.get(Calendar.MONTH) + 1, '0').append('-');
appendTwoDigits(result, c.get(Calendar.DATE), '0').append('T');
appendTwoDigits(result, c.get(Calendar.HOUR_OF_DAY), '0').append(':');
...