Java Date + TimzeZone + Locale + DateFormat


Time in Java Date is the number of milliseconds since January 1, 1970, 00:00:00 GMT, it's timezone-agnostic. (the getYear/Month/Date are dprecated because they are timezone-related.)

When we format the date to a string, we need specify what timezone and what locale to use.
If not specified, it will use time zone and locale in current system.

So if we use the number, time zone doesn't matter. But if we store the deate string or use it to query, we should always use UTC and locale incenstive SimpleDateFormat  to format date object.
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ROOT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
public static void main(final String[] args) {
    TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", new Locale("en", "US"));
    Date date = new Date();
    // 1456470936828 PST(-8:00):2016-02-25T23:15:36.828Z
    System.out.println(date.getTime() + " PST(-8:00):" + sdf.format(date));
    System.out.println(date.getDate());// 25

    TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
    sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", new Locale("en", "US"));
    date = new Date();
    // 1456470936829 EST(-5:00):2016-02-26T02:15:36.829Z
    System.out.println(date.getTime() + " EST(-5:00):" + sdf.format(date));
    System.out.println(date.getDate()); // 26

    // use Locale.ROOT, UTC
    sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ROOT);
    // 1456470936829 UTC:2016-02-26T07:15:36.829Z
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println(date.getTime() + " UTC:" + sdf.format(date));
    System.out.println(date.getDate()); // 26

    // use org.apache.solr.common.util.DateUtil
    // 2016-02-26T07:15:36.829Z
    System.out.println(DateUtil.getThreadLocalDateFormat().format(date));
}
org.apache.solr.common.util.DateUtil
public static DateFormat getThreadLocalDateFormat() {
  return fmtThreadLocal.get();
}

public static TimeZone UTC = TimeZone.getTimeZone("UTC");
private static ThreadLocalDateFormat fmtThreadLocal = new ThreadLocalDateFormat();

private static class ThreadLocalDateFormat extends ThreadLocal<DateFormat> {
  DateFormat proto;

  public ThreadLocalDateFormat() {
    super();
    //2007-04-26T08:05:04Z
    SimpleDateFormat tmp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ROOT);
    tmp.setTimeZone(UTC);
    proto = tmp;
  }

  @Override
  protected DateFormat initialValue() {
    return (DateFormat) proto.clone();
  }
}

Solr removes DateUtil in later release. We can use Apache HttpClient org.apache.http.client.utils.DateUtils.parseDate(String, String[]) to parse the date using multiple date formats.
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)