如何将“白天的片段”转换为Java时间?

[英]How do I convert “fraction of day” to Java time?


I'm working with the Excel format where

我正在使用Excel格式

0.6549

0.6549

Means that we are 65.49% through the day. How would I convert this to a nice looking time such as

也就是说,我们每天都是65。49%如何将它转换成一个漂亮的时间,比如

15:30:42 PM

15:30:42点

in Java?

在Java中?

1 个解决方案

#1


4  

Try this:

试试这个:

public static void main(String[] args) throws Exception {
    DateFormat format = new SimpleDateFormat("HH:mm:ss a");
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    for(String fracStr : args) {
        double frac = Double.parseDouble(fracStr);
        long day = (long) Math.floor(frac);
        frac = frac - day;
        Date time = new Date((long) ( 86400000l * frac));
        System.out.println(fracStr+" -> "+format.format(time));
    }
}

A little note here - the GMT time zone may not get you the results you expect, but UTC will. This is because Java considers GMT as a valid abbreviation for Europe/London and between 1968 and 1972 London was on permanent daylight saving. So midnight 1st January 1970 GMT (the Java epoch) is also 1am 1st January 1970 (GMT).

这里有一个小提示——GMT时区可能不会得到您预期的结果,但是UTC会。这是因为Java认为GMT常量是欧洲/伦敦的有效缩写,在1968年至1972年间,伦敦一直在使用夏令时。所以,1970年1月1日午夜也就是1970年1月1日凌晨1点。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/04/12/53812e2e84a4a8ee3ad76d3e8b1be0e7.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号