简体   繁体   English

java.util.Date得到毫秒

[英]java.util.Date get milliseconds

I have done the following: 我做了以下事情:

String standardRange = "00:01:01";
SimpleDateFormat rangeFormatter = new SimpleDateFormat("hh:mm:ss");
Date range = rangeFormatter.parse(standardRange);

Now: 现在:

range.getTime();

.. I get the output of -3539000 and not 61,000 ..我的输出为-3539000而不是61,000

I'm not sure what I'm doing wrong; 我不确定我做错了什么; when debugging, cdate exists, the attribute contains a fraction , which contains the value 61,000, which is what I want. 在调试时, cdate存在,该属性包含一个fraction ,其中包含值61,000,这就是我想要的。

The reason you're seeing this is that the date you're creating is actually in the past of the date epoch, not 1m1s after it: 你看到这个的原因是你创建的日期实际上是在日期时代的过去,而不是它之后的1m1s:

String standartRange = "00:01:01";
SimpleDateFormat rangeFormatter = new SimpleDateFormat("hh:mm:ss");
Date range = rangeFormatter.parse(standartRange);

System.out.println(new Date(0L));
System.out.println(new Date(0L).getTime());
System.out.println(range);
System.out.println(range.getTime());

and its output; 及其产出;

Thu Jan 01 01:00:00 GMT 1970
0
Thu Jan 01 00:01:01 GMT 1970
-3539000

The epoch date is incorrect here - it should be 00:00:00, but due to a historical bug where BST/GMT changed dates and timezone cant keep track. 这里的纪元日期不正确 - 它应该是00:00:00,但由于BST / GMT更改日期和时区的历史错误无法跟踪。 It seems that Sun/Oracle consider this a historical "inaccuracy". 似乎Sun / Oracle认为这是一个历史性的“不准确”。

Check out the bug report - its describes the problem more fully. 查看错误报告 - 它更全面地描述了问题。

From your language (German) this may not be directly due to this BST issue, but its almost certainly related. 根据您的语言(德语),这可能不是由于这个BST问题直接导致的,但它几乎肯定是相关的。

Java Date is not designed to calculate the duration of a given time period. Java Date不是为计算给定时间段的持续时间而设计的。 The getTime() call returns the numbers of milliseconds since January 1, 1970, 00:00:00 GMT. getTime()调用返回自1970年1月1日00:00:00 GMT以来的毫秒数。 In your case you are actually ending up with a date that comes before that epoch (thus the negative number). 在你的情况下,你实际上最终会得到一个在该纪元之前的日期(因此是负数)。 When I run your code I get 21661000. (See the answer from Sean Landsman as I believe he has hit on why you get the negative results...hint: my number is exactly 6 hours off of GMT or 21600000ms) 当我运行你的代码时,我得到了21661000.(请参阅Sean Landsman的答案,因为我相信他已经找到了为什么会得到负面结果...提示:我的号码正好是格林尼治标准时间的6小时或21600000毫秒)

Joda-Time is a library that is well suited to solving your underlying problem. Joda-Time是一个非常适合解决您的潜在问题的库。

PeriodFormatter formatter = new PeriodFormatterBuilder()
         .appendHours()
         .appendSeparator(":")
         .appendMinutes()
         .appendSeparator(":")
         .appendSeconds()
         .toFormatter();
Period period = formatter.parsePeriod("00:01:01");
assert period.toStandardDuration().getMillis() == 61000

To get what you want, you should compare between the time you want and origin of the time. 为了得到你想要的东西,你应该比较你想要的时间和时间的来源。 using the below code: 使用以下代码:

String standardRange = "00:01:01";
SimpleDateFormat rangeFormatter = new SimpleDateFormat("HH:mm:ss");
Date range = rangeFormatter.parse(standardRange);
Date range2 = rangeFormatter.parse("00:00:00");
System.out.println(range.getTime() - range2.getTime());

According to the JavaDoc, getTime() : 根据JavaDoc, getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. 返回自此Date对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。

You want the number of milliseconds in one minute and one second. 您想要一分钟和一秒钟内的毫秒数。

(60*minutes+seconds)*1000

It really doesn't need to come from a Date object. 它实际上不需要来自Date对象。

If you need to compute the time in milliseconds for some interval, maybe use the joda time library, or get the day, hour, minute, second and millisecond components out of your date object and compute the value by hand. 如果您需要计算某个时间间隔的时间(以毫秒为单位),可以使用joda时间库,或者从日期对象中获取日,小时,分钟,秒和毫秒组件并手动计算值。

Try: 尝试:

Date range1 = rangeFormatter.parse("00:01:01");
Date range2 = rangeFormatter.parse("00:00:00");
System.out.println(range1.getTime() - range2.getTime());

hh:mm:ss stands for 12-hour time, which always stands for "time point", not "time interval". hh:mm:ss代表12小时的时间,它总是代表“时间点”,而不是“时间间隔”。 So surely, time zone will effect the value. 所以,时区肯定会影响价值。 However, in GMT +0 the value equals to which represents "time interval". 但是,在GMT +0中,该值等于表示“时间间隔”。

All you just need is: 你只需要:

rangeFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));

Try it! 试试吧!

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

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