DateUtil

public class DateUtil

An utility for converting from java datetime to UTC conformed datetime and vice versa.

  1. Fixed a bug that it parses UTC datetime with ‘Z’ timezone incorrectly becasue it used wrong timezone (GMT, but not UTC). (Thank Martin Kalen for figure out this)
  2. Added datetime / calendar two UTC convertion.
Author:Twinsen Tsang, (modifier Philip Wong)

Methods

GMT2Calender

public static Calendar GMT2Calender(String dateTime)

Convert an GMT representation of string dateTime to java calendar object.

Parameters:
  • dateTime – A string representing a GMT datetime.
Throws:
Returns:

The millisecond representing the dateTime

GMT2Date

public static Date GMT2Date(String dateTime)

Convert an GMT representation of string dateTime to java Date object.

Parameters:
  • dateTime – A string representing a GMT datetime.
Throws:
Returns:

A java date object representing the dateTime

GMT2MS

public static long GMT2MS(String dateTime)

Convert an GMT representation of string dateTime to millisecond.

Parameters:
  • dateTime – A string representing a GMT datetime.
Throws:
Returns:

The millisecond representing the dateTime

GMT2Timestamp

public static Timestamp GMT2Timestamp(String dateTime)

Convert an GMT representation of string dateTime to java Timestamp object.

Parameters:
  • dateTime – A string representing a GMT datetime.
Throws:
Returns:

A java timestamp object representing the dateTime

UTC2Calendar

public static Calendar UTC2Calendar(String dateTime)

Convert an UTC representation of string dateTime to java calendar object. Followings are UTC conformed patterns:

  • 2007-07-16T13:24:56
  • 2007-07-16T13:24:56Z
  • 2007-07-16T13:24:56.789
  • 2007-07-16T13:24:56.789Z
  • 2007-07-16T13:24:56+02:00
  • 2007-07-16T13:24:56-02:00
  • 2007-07-16T13:24:56.789+02:00
  • 2007-07-16T13:24:56.789-02:00
Parameters:
  • dateTime – A string representing a UTC datetime.
Throws:
Returns:

A java clendar object representing the dateTime

UTC2Date

public static Date UTC2Date(String dateTime)

Convert an UTC representation of string dateTime to java Date object. Followings are UTC conformed patterns:

  • 2007-07-16T13:24:56
  • 2007-07-16T13:24:56Z
  • 2007-07-16T13:24:56.789
  • 2007-07-16T13:24:56.789Z
  • 2007-07-16T13:24:56+02:00
  • 2007-07-16T13:24:56-02:00
  • 2007-07-16T13:24:56.789+02:00
  • 2007-07-16T13:24:56.789-02:00
Parameters:
  • dateTime – A string representing a UTC datetime.
Throws:
Returns:

A java date object representing the dateTime.

UTC2MS

public static long UTC2MS(String dateTime)

Convert an UTC representation of string dateTime to millisecond. Followings are UTC conformed patterns:

  • 2007-07-16T13:24:56
  • 2007-07-16T13:24:56Z
  • 2007-07-16T13:24:56.789
  • 2007-07-16T13:24:56.789Z
  • 2007-07-16T13:24:56+02:00
  • 2007-07-16T13:24:56-02:00
  • 2007-07-16T13:24:56.789+02:00
  • 2007-07-16T13:24:56.789-02:00
Parameters:
  • dateTime – A string representing a UTC datetime.
Throws:
Returns:

The millisecond representing by dateTime.

UTC2Timestamp

public static Timestamp UTC2Timestamp(String dateTime)

Convert an UTC representation of string dateTime to java Timestamp object. Followings are UTC conformed patterns:

  • 2007-07-16T13:24:56
  • 2007-07-16T13:24:56Z
  • 2007-07-16T13:24:56.789
  • 2007-07-16T13:24:56.789Z
  • 2007-07-16T13:24:56+02:00
  • 2007-07-16T13:24:56-02:00
  • 2007-07-16T13:24:56.789+02:00
  • 2007-07-16T13:24:56.789-02:00
Parameters:
  • dateTime – A string representing a UTC datetime.
Throws:
Returns:

A java timestamp object representing the dateTime.

calendar2UTC

public static String calendar2UTC(Calendar dateTime)

Convert a calendar dateTime to UTC conformed representation of string.

Parameters:
  • dateTime – A java calendar representing the time you want to convert.
Throws:
Returns:

an UTC conformed representation with time specified by dateTime

date2UTC

public static String date2UTC(Date dateTime, TimeZone timeZone)

Convert a date dateTime to UTC conformed representation of string. (e.g. 2007-07-16T13:24:56.789+13:00)

Parameters:
  • dateTime – A java date representing the time you want to convert.
  • timeZone – The timezone of the dateTime. If it is null, it use the default timezone in the machine.
Throws:
Returns:

an UTC conformed representation with time specified by dateTime

getCurrentUTCDateTime

public static String getCurrentUTCDateTime()
Returns:Get the current datetime with respect to the default timezone in UTC conformed representation.