Anton Derevyanko
Anton Derevyanko

Reputation: 3445

SimpleDateFormat.parse returns Date in different timezones

There are many issues with Date and Calendar on android and I'm catch another one. I have to parse few dates (stores as strings) to Date. I wrote method:

private Date getDateFromString(String dateStr, String timeStr) {
 Date dateObj = null;
 SimpleDateFormat dateFormater = new SimpleDateFormat("dd. MMM yyyy HH:mm", new Locale("da", "DK"));
 dateFormater.setTimeZone(TimeZone.getTimeZone("GMT+01:00"));
  try {
   dateObj = dateFormater.parse(dateStr + " " + timeStr);
  } catch (ParseException parseExc) {
   parseExc.printStackTrace();
 }
 return dateObj;
}

and it works, but when I try to log result, I saw next output:

11-14 15:49:03.223: D/my date(560): Sun Mar 04 08:00:00 GMT+01:00 2012
11-14 15:49:03.223: D/my date(560): Sun Mar 11 08:00:00 GMT+01:00 2012
11-14 15:49:03.233: D/my date(560): Sun Mar 18 08:00:00 GMT+01:00 2012
11-14 15:49:03.233: D/my date(560): Sun Mar 25 08:00:00 GMT+02:00 2012
11-14 15:49:03.243: D/my date(560): Sun Apr 01 08:00:00 GMT+02:00 2012

i.e. some dates are parsed into GMT+1 timezone and some dates into GMT+2. How can I obtain all dates into same timezone?

Upvotes: 1

Views: 551

Answers (1)

Jeff Gilfelt
Jeff Gilfelt

Reputation: 26159

Denmark switches to from Central European Standard Time (GMT+1) to Central European Summer Time (GMT+2) on 25 March 2012. If the data you are working with does not reflect these daylight savings changes I would suggest not supplying the Danish locale to your SimpleDateFormat instance:

SimpleDateFormat dateFormater = new SimpleDateFormat("dd. MMM yyyy HH:mm");

Upvotes: 2

Related Questions