将毫秒添加到 Java 日期,当毫秒很长时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3453590/
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-08-14 00:55:38  来源:igfitidea点击:

Add milliseconds to Java date, when milliseconds is long

javadatetimedate

提问by cweston

I am looking for the best way to add milliseconds to a Java Date when milliseconds is stored as a 'long'. Java calendar has an addfunction, but it only takes an 'int' as the amount.

当毫秒被存储为“长”时,我正在寻找将毫秒添加到 Java 日期的最佳方法。Java 日历有一个add函数,但它只需要一个 'int' 作为数量。

This is one solution I am proposing...

这是我提出的一种解决方案......

Calendar now = Calendar.getInstance();
Calendar timeout = Calendar.getInstance();

timeout.setTime(token.getCreatedOn());
timeout.setTimeInMillis(timeout.getTimeInMillis() + token.getExpiresIn());

Any other suggestions?

还有其他建议吗?

采纳答案by Jon Skeet

Your solution looks nearlyfine to me, actually. I originally posted an answer going via Date, when I hadn't taken getTimeInMillisand setTimeInMillisinto account properly.

实际上,您的解决方案在我看来几乎没问题Date当我没有正确考虑getTimeInMillissetTimeInMillis考虑时,我最初通过 发布了一个答案。

However, you're calling setTimeand thensetTimeInMilliswhich seems somewhat redundant to me. Your code looks equivalent to this:

但是,您正在打电话setTime然后setTimeInMillis这对我来说似乎有些多余。您的代码看起来相当于:

Calendar timeout = Calendar.getInstance();
timeout.setTimeInMillis(token.getCreatedOn().getTime() + token.getExpiresIn());

A generally nicer alternative would be to use Joda Timethough :) It's generally a muchnicer date/time API.

通常一个更好的替代方法是使用约达时间虽然:)这通常是一个很大更好的日期/时间API。

回答by Dubas

You can also create a date with the current local date plus the number of miliseconds you need to add as Expire time

您还可以使用当前本地日期加上您需要添加为到期时间的毫秒数来创建日期

import java.util.Date;

long expiremilis = 60000l; //  1 minute
// Expires in one minute from now
Date expireDate = new Date(System.currentTimeMillis() + expiremilis);

Or the same with a calendar

或与日历相同

long expiremilis = 60000l; // 1 minute
Calendar expireDate= Calendar.getInstance();
// Expires on one minute from now
expireDate.setTimeInMillis(System.currentTimeMillis() + expiremilis);

If you use an existing Date object you can do:

如果您使用现有的 Date 对象,您可以执行以下操作:

import java.util.Date;

long expiremilis = 60000l; // 1 minute
// Expires on one minute from the date object date
Date expireDate = new Date(myDate.getTime() + expiremilis);

And with an existing Calendar Object

并使用现有的日历对象

long expiremilis = 60000l; // 1 minute
Calendar expireDate= Calendar.getInstance();
// Expires on one minute from the calendar date
expireDate.setTimeInMillis(myCalendar.getTimeInMillis() + expiremilis);

回答by Peter Lawrey

Calendar is a fairly expensive Date object, and it functionality is not the best. If you want an all purpose Date object I suggest looking at JODA Timeand it has a function which does what you want.

Calendar 是一个相当昂贵的 Date 对象,它的功能并不是最好的。如果您想要一个多用途的 Date 对象,我建议您查看JODA Time并且它有一个功能可以满足您的需求。

However, a simpler Java Date object is the Date class as @Dubas indicates.

但是,更简单的 Java Date 对象是 @Dubas 指出的 Date 类。

Even simpler for the type of operation you are performing is to use a long. This is also much faster.

对于您正在执行的操作类型,更简单的是使用 long。这也快得多。

long timeoutMS = token.getCreatedOn() + token.getExpiresIn(); 

I use long (in GMT) for all my dates and only use Date for the presentation layer. ie. when you want to convert the date to Text.

我对所有日期都使用 long(在 GMT 中),并且只对表示层使用 Date。IE。当您想将日期转换为文本时。