Java - 将 JLabel 解析为 java.util.Date

Java - Parsing a JLabel to java.util.Date

我只是想使用 simpleDateFormatter() 将 JLabel 中的字符串解析为日期。基于我在网上搜索的所有内容,这段代码应该可以工作。但是,我在编译期间收到 "cannot find symbol - method parse(java.lang.String)" 错误。非常感谢任何有关如何解决此问题的建议。

有问题的 JLabel 使用 JDBC.

数据库查询中的日期填充

此外,我知道 java.util.Date 已被弃用,但仍想为此使用它。

代码片段:

private Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
private JLabel dateDataLabel = new JLabel("");

private void setAndParseLabel()
{
    dateDataLabel.setText(formatter.format(validatePass.eventDate));

    java.util.Date aDate = formatter.parse(dateDataLabel.getText());
}

java.text.Format 没有方法 parse,因此代码无法编译。

您可以参考java.text.DateFormat:

private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

java.text.Format 中没有方法 parse。使用 java.text.DateFormat 代替:

private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

tl;博士

  • 你忽略了关键的时区问题。您无意中将输入解析为 UTC 中的值。
  • 您使用的是多年前被取代的可怕的旧日期时间 类。请改用 java.time

示例代码:

LocalDateTime
.parse( 
    "2018-01-23 13:45".replace( " " , "T" )  // Comply with standard ISO 8601 format by replacing SPACE with `T`. Standard formats are used by default in java.time when parsing/generating strings.
)                                            // Returns a `LocalDateTime` object. This is *not* a moment, is *not* a point on the timeline.
.atZone(                                     // Apply a time zone to determine a moment, an actual point on the timeline.
    ZoneId.of( "America/Montreal" ) 
)                                            // Returns a `ZonedDateTime` object.
.toInstant()                                 // Adjust from a time zone to UTC, if need be.

java.time

现代方法使用java.time类。

您输入的字符串几乎是标准的 ISO 8601 格式。要完全遵守,请将中间的 SPACE 替换为 T.

String input = "2018-01-23 13:45".replace( " " , "T" ) ;

解析为 LocalDateTime 因为您的输入没有时区指示符或与 UTC 的偏移量。

LocalDateTime ldt = LocalDateTime.parse( input ) ;

A LocalDateTime 根据定义 not 代表一个时刻,not 是时间轴上的一个点。它表示大约 26-27 小时(全球时区范围)范围内的 潜在 时刻。

要确定一个时刻,分配一个时区(ZoneId)得到一个ZonedDateTime对象。

指定 proper time zone name in the format of continent/region, such as America/Montreal, Africa/CasablancaPacific/Auckland。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们 不是 真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;

如果您希望通过 UTC 挂钟时间查看同一时刻,请提取 Instant

Instant instant = zdt.toInstant() ;  // Adjust from some time zone to UTC.

尽可能避免 java.util.Date。但如果你必须与尚未更新到java.time的旧代码进行互操作,你可以来回转换。调用添加到旧 类.

的新转换方法
java.util.Date d = java.util.Date.from( instant ) ;  // Going the other direction: `myJavaUtilDate.toInstant()`

关于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.

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

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

您可以直接与数据库交换 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.