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

标签 java date datetime simpledateformat

我只是尝试使用 simpleDateFormatter() 将 JLabel 中的字符串解析为日期。根据我在网上搜索的所有内容,这段代码应该可以工作。但是,我在编译过程中收到“找不到符号 - 方法解析(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());
}

最佳答案

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 格式。要完全遵守,请将中间的空格替换为 T

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

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

LocalDateTime ldt = LocalDateTime.parse( input ) ;

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

要确定时刻,请分配时区 (ZoneId) 以获取 ZonedDateTime 对象。

指定proper time zone name格式为大洲/地区,如America/Montreal , Africa/Casablanca ,或太平洋/奥克兰。切勿使用 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框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧类 legacy日期时间类,例如 java.util.Date , Calendar , & SimpleDateFormat .

Joda-Time项目,现在位于 maintenance mode ,建议迁移到java.time类。

要了解更多信息,请参阅 Oracle Tutorial 。并在 Stack Overflow 上搜索许多示例和解释。规范为JSR 310 .

您可以直接与数据库交换java.time对象。使用JDBC driver符合JDBC 4.2或稍后。不需要字符串,不需要 java.sql.* 类。

从哪里获取java.time类?

ThreeTen-Extra项目通过附加类扩展了 java.time。该项目是 java.time future 可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如 Interval , YearWeek , YearQuarter ,和 more .

关于Java - 将 JLabel 解析为 java.util.Date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52579931/

相关文章:

sql - 更新 SQL Server 中的所有日期列 -1 天

c# - C#Finisar SQLite日期时间比较问题

java - 如何使用纯 Java 流式传输 mp3

sql - 计算一个日历周期内多个起止记录之间的天数

php - 这是 PHP 的 DateTime :diff? 中的错误吗

sql-server - DateTime 与 DateTime2 时间范围差异

java - 如何使用 Java 为 Leap Motion 编程

java - 动态更新观察者中的饼图?

java - 如何为未配置 web.xml 的 Web 应用程序指定显示名称

mysql - 将sql中的多个epoch时间一起转换为日期