简体   繁体   English

在格式化日期时忽略DST(Dayligt节省时间)

[英]Ignore DST (Dayligt Saving Time) while formating date

I have this application which compares current date to a parsed date from an xml file: 我有这个应用程序,它将当前日期与xml文件中的解析日期进行比较:

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. 问题是当我通过simpleDateFormat将变量timeDiff作为字符串输出时,会考虑夏令时,这会增加额外的小时。 This messes up my output. 这弄乱了我的输出。

Is there anyway to make SimpleDateFormat ignore DST? 反正有没有让SimpleDateFormat忽略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. 它有normalize()方法,它接受一个布尔值来指示你是否要忽略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";
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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