java.util.Date 类的参数化构造函数已弃用。还有什么选择呢?

发布于 2024-08-14 01:23:20 字数 70 浏览 3 评论 0 原文

我可以用什么来替换这个,new Date(2009, 12, 9)

感谢您的帮助。

What can I use to replace this, new Date(2009, 12, 9)?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

饮惑 2024-08-21 01:23:20

注意:这个答案写于 2009 年。从那时起,java.time 就成为 Java 中首选的日期/时间 API。


理想情况下,请使用 Joda 时间。它是一种比内置 API 无限优越的 API。然后,您需要在 LocalDateTime 之间进行选择 DateTime 取决于您的具体要求(这是一个复杂的领域 - 我不会尝试用一两句话来总结,但是 文档 做得很好)。

如果绝对必要,请使用 java.util.Calendar< /a> 并在需要时将其转换为 Date

Note: this answer was written in 2009. Since then, java.time has become the preferred date/time API in Java.


Ideally, use Joda Time instead. It's an infinitely superior API to the built-in one. You'd then want to choose between LocalDateTime and DateTime depending on your exact requirements (it's a complicated area - I'm not going to try to summarise in a sentence or two, but the docs do a good job).

If absolutely necessary, use a java.util.Calendar and convert that to a Date when you need to.

旧人九事 2024-08-21 01:23:20

日历!

Calendar cal = Calendar.getInstance();
cal.set(2009, Calendar.DECEMBER, 12);

请注意,我没有'不要将 12 表示为 12 月,因为它实际上是 11(一月是 0)。

然后,您可以使用以下命令轻松添加或删除秒、分钟、小时、天、月或年:

cal.add(Calendar.HOUR, 2);
cal.add(Calendar.MONTH, -5);

最后,如果您想要日期:

cal.getTime();

Calendar !

Calendar cal = Calendar.getInstance();
cal.set(2009, Calendar.DECEMBER, 12);

Notice that i didn't put 12 for december because it's actually 11 (january is 0).

Then, you can add or remove seconds, minutes, hours, days, months or year easily with :

cal.add(Calendar.HOUR, 2);
cal.add(Calendar.MONTH, -5);

And finally, if you want a Date :

cal.getTime();
若能看破又如何 2024-08-21 01:23:20

如果您查看 Javadoc 它指导您使用日历

从 JDK 1.1 版开始,替换为 Calendar.set(year + 1900, Month,
日期、小时、分钟)或 GregorianCalendar(年 + 1900、月、日期、小时、
分钟)。

如果您查看 Date 构造函数参数,您就会明白为什么它被弃用:

参数:

year - 年份减去 1900。
月份 - 0-11 之间的月份。
日期 - 1-31 之间的月份中的某一天。
hrs - 0-23 之间的时间。
min - 0-59 之间的分钟。

year 不是您所期望的,month 也不是您所期望的。

要表示您提到的日期,您需要像这样调用Date(不推荐)

new Date(2009-1900, 12-1, 9)

使用Calendar的替代方法是

Calendar cal = Calendar.getInstance();
cal.set(2009, 11, 9); //year is as expected, month is zero based, date is as expected
Date dt = cal.getTime();

If you look at the Javadoc it points you towards using Calendar.

As of JDK version 1.1, replaced by Calendar.set(year + 1900, month,
date, hrs, min) or GregorianCalendar(year + 1900, month, date, hrs,
min).

If you look at the Date constructor params you'll see why it was deprecated:

Parameters:

year - the year minus 1900.
month - the month between 0-11.
date - the day of the month between 1-31.
hrs - the hours between 0-23.
min - the minutes between 0-59.

year isn't what you expect and neither is month.

To represent the date you have mentioned you need to call Date like this (not recommended)

new Date(2009-1900, 12-1, 9)

The alternative using Calendar is

Calendar cal = Calendar.getInstance();
cal.set(2009, 11, 9); //year is as expected, month is zero based, date is as expected
Date dt = cal.getTime();
天荒地未老 2024-08-21 01:23:20

您还可以使用 SimpleDateFormat 对象:

import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;


public class DateTest {
    public static void main( String [] args ) throws ParseException {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy, MM, dd");
        Date date = sdf.parse("2009, 12, 9");
        System.out.println( date );
    }
}

You can also use the SimpleDateFormat object:

import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;


public class DateTest {
    public static void main( String [] args ) throws ParseException {
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy, MM, dd");
        Date date = sdf.parse("2009, 12, 9");
        System.out.println( date );
    }
}
睫毛溺水了 2024-08-21 01:23:20

Tim,在您的评论中您提到您是在 GXT 上下文中执行此操作 - 即在 GWT 客户端代码中。 GWT 不支持 GregorianCalendar,您很可能无法通过 GWTCompiler 放置 JodaTime(您也许可以,但您真的想这样做吗)。

我认为如果您想在 GWT 中进行日历操作,您确实可以选择使用 JNSI。请参阅 JavaScript 中的 Date 类。

Tim, in your comments you mentioned that you are doing this in a GXT context - i.e. in GWT client code. GWT does not support GregorianCalendar and you will most likely not be able to put JodaTime through the GWTCompiler (you may be able to, but do you really want to).

I think you are left really with the option to using JNSI if you want to do calendar operations in GWT. See the Date class in JavaScript.

场罚期间 2024-08-21 01:23:20

java.time

java.util Date-Time API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用它们并切换到 现代日期时间 API*

另外,下面引用的是 Joda-Time 主页上的通知:

请注意,从 Java SE 8 开始,用户被要求迁移到 java.time (JSR-310) - JDK 的核心部分,它将取代该项目。

使用现代 API java.time 的解决方案:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = LocalDate.of(2009, 12, 9).atStartOfDay(ZoneId.of("Etc/GMT"));
        System.out.println(zdt);
    }
}

输出:

2009-12-09T00:00Z[Etc/GMT]

输出中的 Z零时区偏移的时区指示符。它代表 Zulu,指定 Etc/UTC 时区(时区偏移量为 +00:00 小时)。

无论出于何种原因,如果您需要将 ZonedDateTime 对象转换为 java.util.Date 对象,可以按如下方式操作:

Instant instant = zdt.toInstant();
Date date = Date.from(instant);

在线演示

就像java.util.Date一样,一个Instant 表示UTC 中的时间线,但与 java.util.Date 不同,它并不代表真正的日期时间对象(java.util.Date 对象仅代表自标准基准时间(称为“纪元”)以来的毫秒数,即 January 1 , 1970, 00:00:00 GMT),它代表一个真实的日期时间对象。

了解有关 java.time 的更多信息,现代日期时间 API* 来自 跟踪:日期时间


* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API如何在Android项目中使用ThreeTenABP

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice at the Home Page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern API:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = LocalDate.of(2009, 12, 9).atStartOfDay(ZoneId.of("Etc/GMT"));
        System.out.println(zdt);
    }
}

Output:

2009-12-09T00:00Z[Etc/GMT]

The Z in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

For any reason, if you need to convert this object of ZonedDateTime to an object of java.util.Date, you can do so as follows:

Instant instant = zdt.toInstant();
Date date = Date.from(instant);

ONLINE DEMO

Like java.util.Date, an Instant represents an instantaneous point on the timeline in UTC, but unlike java.util.Date, which does not represent a real Date-Time object (a java.util.Date object simply represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT), it represents a real Date-Time object.

Learn more about java.time, the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

肤浅与狂妄 2024-08-21 01:23:20

不幸的是,Java 中的日期支持非常糟糕。正式地,您可能必须使用日历来执行此操作,但在我看来这不是必需的。正如其他人提到的,Joda 时间要好得多,但仍然不如 Ruby on Rails 中的日期那么容易使用。

我不知道有哪个 Java 包可以为您提供如此多的日期支持(Joda 稍有不足,但已经很接近了),但是在 Groovy 中,使用 TimeCategory 可以为您提供非常类似于 Ruby on Rails 的日期支持。

Unfortunately date support in Java is completely awful. Officially, you'd probably have to do this with Calendar, but that shouldn't be necessary in my opinion. Like others have mentioned, Joda time is a lot better, but still not quite as easy to use as dates in Ruby on Rails.

I'm not aware of any Java package that gives you quite that amount of date support (Joda falls short a bit, but comes close), but in Groovy, using TimeCategory gives you very Ruby on Rails-like date support.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文