0

I'm having a time zone issue with my SimpleDateFormat. Here is my code:

TextView date_time = (TextView) view.findViewById(R.id.date_time);
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Date date = null;
try {
    date = df.parse(workout.getDate().toString());
    System.err.println("position: " + position + " workout: " + workout.getDate().toString());
    System.err.println("position: " + position + " date: " + date);
} catch (ParseException e) {
    e.printStackTrace();
}
df = new SimpleDateFormat("EEEE, MMMMM dd", Locale.US);
date_time.setText(df.format(date));

Note this output:

position: 0 workout: Mon Aug 19 00:00:00 MDT 2013
position: 0 date: Sun Aug 18 23:00:00 MDT 2013

Do you see how the string starts (correctly) with the date being Aug 19 (at midnight). But then after the date formatter does its work, I come out with the time being 1 hour earlier. I'm assuming that this is some time zone manipulation, but I don't know how to correct for it. I tried some different values for 'Z' (including 'Z', 'ZZZ', 'ZZZZ', and 'ZZZZZ'), but all give the same result. I assume that it's a time zone problem, but in both cases is shows 'MDT', so maybe not.

How do I stop this one hour shift from happening? Thanks!

EDIT: VERY hacky solution but it works:

try {
    date = df.parse(workout.getDate().toString());
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.HOUR_OF_DAY, 1);
    date = cal.getTime();
} catch (ParseException e) {
    e.printStackTrace();
}
AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • Rather than using `toString()` to format the date (which you are also implicitly using in the second print statement), how about using the same `SimpleDateFormat` that you are using to parse it, i.e. using `df.format(date)`? Also, which of the values is the correct one? – drmirror Aug 21 '13 at 03:33
  • I'm not sure that I'm following you. Ultimately, the whole purpose of this code is to present the date in a TextView in the format `Tuesday, August 20`. (I may well be taking a circuitous route, and I'm open to suggestions, for sure.) So, if I use the same date format, then I accomplish nothing (at best). However, for testing purposes, I commented out the second to last line. I still get the one-hour shift, but of course now the TextView doesn't format correctly. – AndroidDev Aug 21 '13 at 03:55
  • Your problem almost certainly has to do with incorrect handling of daylight saving time somewhere along the road. Your hacky solution will probably break down once you cross a DST/non-DST line, so I'd be very careful with it. Apart from that, I may have a hard time understanding your code because it is not clear what `workout` exactly is. Is it a `Date`? Or a `String`? – drmirror Aug 21 '13 at 21:18
  • Workout is a custom class that has a Date field called date. The getDate() method retrieves this value. Good idea about DST. I'll look into it. – AndroidDev Aug 21 '13 at 21:41

0 Answers0