从 Java 日期中添加/减去 5 秒 - 显示已弃用警告

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

Adding/Subtracting 5 seconds from Java Date - Showing deprected warning

javadatetime

提问by Syed Rahman Mashwani

I want to add 5 seconds to current time

我想为当前时间增加 5 秒

    Date date = new Date();

    date.setSeconds(date.getSeconds()+ 5);
    System.out.println("old Value is: "+date);
    System.out.println("New Value is: "+ date);

It generates the correct output exactly what I needed as:

它生成的输出正是我需要的:

Old Value is: Thu Apr 17 14:10:33 PKT 2014
New Value is: Thu Apr 17 14:10:38 PKT 2014

but it gives me a warning error message as

但它给了我一条警告错误消息

Multiple markers at this line
    - The method getSeconds() from the type Date is deprecated
    - The method setSeconds(int) from the type Date is 
     deprecated

What this means. Is it safe to ignore this warning? if not then how to handle it?

这是什么意思。忽略此警告是否安全?如果不是那么如何处理呢?

回答by Petr Mensik

deprecatedmeans that this method should't be used anymore because it can be removed from the Java language in favor of some other method (it probably won't be removed but it's not the preferable way of doing things anymore). Documentationsuggests you to use Calendar.get(Calendar.SECOND)which is what you should use in your code.

deprecated意味着不应再使用此方法,因为它可以从 Java 语言中删除以支持某些其他方法(它可能不会被删除,但它不再是首选的处理方式)。文档建议您使用Calendar.get(Calendar.SECOND)您应该在代码中使用的内容。

Note that from Java 8 you can use LocalDateTime#getSecondmethod again.

请注意,从 Java 8 开始,您可以LocalDateTime#getSecond再次使用方法。

回答by andy

you can use date.setTime(date.getTime() + 5000)

您可以使用 date.setTime(date.getTime() + 5000)

回答by Sanjay Rabari

showing deprecated warning that means there is some betterway available in java to do so.

显示已弃用的警告,这意味着在 Java 中有一些更好的方法可以这样做。

回答by Philipp Sander

Java 8has completly different time-api and makes handling dates a lot easier. Here's an article

Java 8具有完全不同的 time-api,并且使处理日期变得更加容易。这是一篇文章

With Java Versions 5-7(everything below should not be used IMHO) you should use Calendar (as Petr suggested) If you are allowed to use third-party APIs, you should definitly take a look at JodaTime

随着Java的版本5-7(下面的一切不应该被用来恕我直言),你应该使用日历(如切赫建议)如果你被允许使用第三方API,你应该definitly看看JodaTime

回答by Ruchira Gayan Ranaweera

You can try this. Calendar is the best solution you are looking at.

你可以试试这个。日历是您正在寻找的最佳解决方案。

Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                             .parse("2014-04-17 14:53:25");
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.SECOND,(calendar.get(Calendar.SECOND)-5));

System.out.println(calendar.getTime());

Out put:

输出:

Thu Apr 17 14:53:20 IST 2014

回答by Basil Bourque

FYI, here is some example code using Joda-Time2.3. The bundled java.util.Date and .Calendar classes are notoriously troublesome, and should be avoided.

仅供参考,这里是一些使用Joda-Time2.3 的示例代码。捆绑的 java.util.Date 和 .Calendar 类是出了名的麻烦,应该避免。

Unlike a java.util.Date, a Joda-Time DateTimeobject has an assigned time zone.

与 java.util.Date 不同,Joda-Time DateTime对象具有指定的时区。

When specifying time zones, use a proper time zone name. Avoid the three or four letter codes as they are neither standardized nor unique.

指定时区时,请使用正确的时区名称。避免使用三个或四个字母的代码,因为它们既不标准化也不独特。

java.util.Date date = new java.util.Date(); // Has no time zone. Misnamed, as it contains both a date and a time portions.

DateTimeZone timeZone = DateTimeZone.forID( "Asia/Karachi" );
DateTime dateTime = new DateTime( date, timeZone );
DateTime dateTimeUtc = dateTime.withZone( DateTimeZone.UTC );
DateTime fiveSecondsAgo = dateTime.minusSeconds( 5 );
java.util.Date date2 = fiveSecondsAgo.toDate();

Dump to console…

转储到控制台...

System.out.println( "date: " + date );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "fiveSecondsAgo: " + fiveSecondsAgo );
System.out.println( "date2: " + date2 );

When run…

运行时…

date: Thu Apr 17 14:44:01 PDT 2014
dateTime: 2014-04-18T02:44:01.773+05:00
dateTimeUtc: 2014-04-17T21:44:01.773Z
fiveSecondsAgo: 2014-04-18T02:43:56.773+05:00
date2: Thu Apr 17 14:43:56 PDT 2014

回答by 6324

An easy way is to use apache DateUtils.

一个简单的方法是使用 apache DateUtils

import org.apache.commons.lang.time.DateUtils;

DateUtils.addSeconds(now, 5); // Add 5 seconds.
DateUtils.addSeconds(now, -5); // Subtracting 5 seconds

You can also add/subtract minutes, hours, and so on...

您还可以添加/减去分钟、小时等...