java.util.Date 获取毫秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15161510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:41:06  来源:igfitidea点击:

java.util.Date get milliseconds

javadatetime

提问by Eddie Texas

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, cdateexists, the attribute contains a fraction, which contains the value 61,000, which is what I want.

我不确定我做错了什么;调试时,cdate存在,该属性包含一个fraction,其中包含值 61,000,这正是我想要的。

回答by Sean Landsman

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 bugwhere BST/GMT changed dates and timezone cant keep track. It seems that Sun/Oracle consider this a historical "inaccuracy".

这里的纪元日期不正确 - 它应该是 00:00:00,但由于BST/GMT 更改日期和时区无法跟踪的历史错误。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 问题造成的,但几乎可以肯定是相关的。

回答by purgatory101

Java Date is not designed to calculate the duration of a given time period. The getTime() call returns the numbers of milliseconds since January 1, 1970, 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 Landsmanas I believe he has hit on why you get the negative results...hint: my number is exactly 6 hours off of GMT or 21600000ms)

Java Date 并非旨在计算给定时间段的持续时间。getTime() 调用返回自 1970 年 1 月 1 日格林威治标准时间 00:00:00 以来的毫秒数。在您的情况下,您实际上以该时代之前的日期结束(因此为负数)。当我运行你的代码时,我得到 21661000。(请参阅Sean Landsman的回答,因为我相信他已经找到了为什么你会得到负面结果......提示:我的数字正好是格林威治标准时间 6 小时或 21600000 毫秒)

Joda-Timeis 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

回答by shuangwhywhy

hh:mm:ssstands for 12-hour time, which always stands for "time point", not "time interval". So surely, time zone will effect the value. However, in GMT +0 the value equals to which represents "time interval".

hh:mm:ss代表 12 小时制,它总是代表“时间点”,而不是“时间间隔”。因此,时区肯定会影响该值。但是,在 GMT +0 中,该值等于 which 代表“时间间隔”。

All you just need is:

您只需要:

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

Try it!

试试看!

回答by Adel Boutros

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());

回答by RudolphEst

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.

返回自 1970 年 1 月 1 日格林威治标准时间 00:00:00 以来由此 Date 对象表示的毫秒数。

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 Dateobject.

它真的不需要来自一个Date对象。

If you need to compute the time in milliseconds for some interval, maybe use the joda timelibrary, or get the day, hour, minute, second and millisecond components out of your date object and compute the value by hand.

如果您需要以毫秒为单位计算某个时间间隔的时间,可以使用joda 时间库,或者从日期对象中获取天、小时、分钟、秒和毫秒组件并手动计算该值。

回答by Evgeniy Dorofeev

Try:

尝试:

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