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"));
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
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