简体   繁体   中英

Ignore DST (Dayligt Saving Time) while formating date

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!

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.

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

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";
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM