2

I have this application which compares current date to a parsed date from an xml file:

Date timeDiff = new Date(date.getTime() - new Date().getTime());

The problem is when i output the variable timeDiff as a string via simpleDateFormat, daylight saving time is taken to account which adds an extra hour. This messes up my output.

Is there anyway to make SimpleDateFormat ignore DST?

Thanks!

skaffman
  • 398,947
  • 96
  • 818
  • 769
Richard
  • 14,427
  • 9
  • 57
  • 85

3 Answers3

3

You're subtracting one time from another, to get a duration in milliseconds. You shouldn't be trying to format that as a date at all. It's a number of milliseconds, not a date.

It's not clear what you expect the result to be, but at the moment you're basically going about it the wrong way.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Well im trying to output the milliseconds as a string, for example: if timeDiff.getTime() is 4500000 i want to output 1h 15m: new SimpleDateFormat("H'h' m'm'").format(timeDiff) – Richard Nov 24 '11 at 19:39
  • @Richard: I'd normally suggest using Joda Time for this (http://joda-time.sf.net) but that *might* be a bit big for your Android app. Have you considered just splitting it into hours, minutes, seconds in code and then just using string formatting? – Jon Skeet Nov 24 '11 at 19:54
1

Why don't you just use Time. It has the normalize() method that takes a boolean to indicate if you want to ignore DST.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

Ok guys! Maybe better with this solution:

public static String millisToString(long l){
        long h, m;
        h = l / 3600000;
        m = (l % 3600000) / 60000;
        if(h == 0){
            return m + "m";
        }else{
            return h + "h " + m + "m";
        }
    }
Richard
  • 14,427
  • 9
  • 57
  • 85