如何在 java 中表示日期

How to represent Date in java

我想要一个具有月、年和日属性的简单日期 class。

我想要一个 Date 对象包含在一个 Magazine 对象中,这样我就可以知道该杂志是什么时候出版的。

当我尝试使用 Date class 中的 Date 对象时,如下所示 日期 d1 = 新日期 (1,1,1); 新日期的日期被删除线表明它已被弃用。 我读到了有关替换它的新构造函数的信息,但它看起来很混乱。 我不明白如何使用日历 class 完成此操作。 如果可能的话,我不想创建自己的日期 class... 请帮我。谢谢

"Deprecated" refers to functions or elements that are in the process of being replaced by newer ones. Deprecated items may work in the current version of a programming language, they may not function in future updates.

但你可以忽略并使用它。

示例:

String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date(1,1,1));
System.out.println(date);

输出:

1901-02-01

但如果您不想使用已弃用的方法,您可以选择 LocalDate :

LocalDate localDate = LocalDate.of(2019, 1, 1);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formatted = localDate.format(dateTimeFormatter);
System.out.println(formatted);

输出:

2019-01-01

tl;博士

I want a Date object to be included in a Magazine object so that I can know when was the Magazine published.

所以添加一个java.time.LocalDate类型的成员变量。

package work.basil.example;

import java.time.LocalDate;

public class Magazine
{
    // Member variables.
    public LocalDate published ;

    // Constructor.
    public Magazine ( LocalDate published )
    {
        this.published = published;
    }
}

使用方法:

Magazine m = new Magazine( LocalDate.of( 2019 , Month.JANUARY , 23 ) ) ;

LocalDate

  • 现代方法使用 LocalDate
  • 切勿使用 java.util.Datejava.sql.Date

LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.

时区对于确定日期至关重要。对于任何给定时刻,日期在全球范围内因地区而异。例如,Paris France is a new day while still “yesterday” in Montréal Québec.

午夜后几分钟

如果未指定时区,JVM 将隐式应用其当前默认时区。该默认值在运行时可能 change at any moment (!),因此您的结果可能会有所不同。最好明确指定您的 desired/expected 时区作为参数。如果关键,请与您的用户确认区域。

Continent/Region的格式指定一个proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 ESTIST 等 2-4 字母缩写,因为它们 不是 真正的时区,未标准化,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

如果你想使用 JVM 当前的默认时区,请求它并作为参数传递。如果省略,代码将变得难以阅读,因为我们不确定您是否打算使用默认值,或者您是否像许多程序员一样没有意识到这个问题。

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

或指定日期。您可以通过数字设置月份,January-December 的编号为 1-12。

LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ;  // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.

或者,更好的是,使用 Month enum objects pre-defined, one for each month of the year. Tip: Use these Month objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety. Ditto for Year & YearMonth

LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

Joda-Time project, now in maintenance mode, advises migration to the java.time 类.

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类.

在哪里获取java.time类?

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.