I have dates strings in the format YYYYMMDD that I am trying to parse into dates using a date formatter obtained as

public static DateFormat getDateFormat() {
  SimpleDateFormat result = new SimpleDateFormat("yyyyMMdd");                  
  result.setLenient(false);
  return result;
}

I set the default time zone when the program runs as

public static void doTheDateZoneInit() {
    TimeZone tzone = TimeZone.getTimeZone("Europe/London");
    TimeZone.setDefault(tzone);
}

When I format the dates and output it without specifying a time zone in the print string

Date myDate= getDateFormat().parse("20110331");
System.out.println("Date after it is formatted:" + myDate);

The output is in BST time zone

Date after it is formatted:Thu Mar 31 01:00:00 BST 2011

If I run the same over and over with different dates I get different output

  • 20120331 >> BST >> Date after it is formatted:Sat Mar 31 01:00:00 BST 2012
  • 20121231 >> GMT >> Date after it is formatted:Mon Dec 31 00:00:00 GMT 2012
  • 20130328 >> GMT >> Date after it is formatted:Thu Mar 28 00:00:00 GMT 2013
  • 20130331 >> GMT >> Date after it is formatted:Sun Mar 31 00:00:00 GMT 2013
  • 20140331 >> BST >> Date after it is formatted:Mon Mar 31 01:00:00 BST 2014
  • 20130401 >> BST >> Date after it is formatted:Mon Apr 01 01:00:00 BST 2013
  • 20130402 >> BST >> Date after it is formatted:Tue Apr 02 01:00:00 BST 2013
  • 20130501 >> BST >> Date after it is formatted:Wed May 01 01:00:00 BST 2013

Seems like there is a range in 2012 to 2013 where all calculates to GMT. I have no idea why this is happening.

The thing is that I add, end-of-day hours to these dates... e.g. I am calling the following method with the dates I converted from String and add 23:59:59:999 to it in order to get the latest time for the date specified.

public static Date addAlmostOneDay(Date startDate) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(startDate);
    cal.add(Calendar.HOUR, 23);
    cal.add(Calendar.MINUTE, 59);
    cal.add(Calendar.SECOND, 59);
    cal.add(Calendar.MILLISECOND, 999);
    return cal.getTime();
}

But in the case where it converted to GMT if I were to add the 23:59:59... it is not setting the date to the end of the original date but takes the time diffs between BST and GMT into account

  • Sun Mar 31 00:00:00 GMT 2013 becomes Mon Apr 01 00:59:59 BST 2013 (the following day + 1hr) while
  • Mon Apr 01 01:00:00 BST 2013 becomes Mon Apr 01 23:59:59 BST 2013 (end of today is what i want)

Can someone please shed some light on why this seems to be hapening. The same code is run with different inputs of YYYMMDD formats?

有帮助吗?

解决方案

OK I think I understood what you meant. You set the default timezone to the one of London.

In the summer, London is at the British Summer Time (BST) timezone. In the winter, it's at the GMT timezone. And the day when this timezone change is done varies from year to year.

其他提示

BST is British summer time and the valid time zone for London between 31 march 2013 - 27 oct 2013, and 30 march 2014 - 26 oct 2014.

Between those dates the selection of Europe/London as a time zone, will give you BST, during winter the correct time zone for London is GMT, which is consistent with your output.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top