views:

40

answers:

1

I have this code:

Date now = new Date();
// the string is in UTC format, so a UTC date must be constructed, I don't know if that happens in this format
Date measure = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(utcDateTime); 
long diff = now.getTime() - measure.getTime();
 if (diff < 1000* 60 * 15) {
   // measure is less then 15 minutes recent
   do some work
 }

When I get the diff, it includes the timezone. I know the Date object internally is UTC.

So what's wrong here?

+3  A: 

While a Date object is indeed in UTC, your SimpleDateFormat may not be. I suspect it default's to the system time zone - that's certainly what experimentation would suggest. You can change this using DateFormat.setTimeZone. So if your text represents a UTC date/time, you should set the time zone of the formatter to UTC as well.

Or you could use Joda Time, which is a generally better date and time API :)

Jon Skeet
Thanks, I will keep in mind Joda Time, although I don't know if available for Java 1.5 and Android.
Pentium10