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();
}