如何转换 UTC 日期字符串并删除 Java 中的 T 和 Z?

新手上路,请多包涵

我正在使用 Java 1.7。

尝试转换:

 2018-05-23T23:18:31.000Z

进入

2018-05-23 23:18:31

日期实用程序类:

 public class DateUtils {

    public static String convertToNewFormat(String dateStr) throws ParseException {
        TimeZone utc = TimeZone.getTimeZone("UTC");
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        sdf.setTimeZone(utc);
        Date convertedDate = sdf.parse(dateStr);
        return convertedDate.toString();
    }
}

尝试使用它时:

 String convertedDate = DateUtils.convertToNewFormat("2018-05-23T23:18:31.000Z");
System.out.println(convertedDate);

得到以下异常:

 Exception in thread "main" java.text.ParseException: Unparseable date: "2018-05-23T23:22:16.000Z"
   at java.text.DateFormat.parse(DateFormat.java:366)
   at com.myapp.utils.DateUtils.convertToNewFormat(DateUtils.java:7)

我可能做错了什么?

有没有更简单的方法(例如 Apache Commons lib)?

原文由 PacificNW_Lover 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 857
2 个回答

尝试这个。您必须使用一种模式进行解析,另一种模式进行格式化。

 public static String convertToNewFormat(String dateStr) throws ParseException {
    TimeZone utc = TimeZone.getTimeZone("UTC");
    SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sourceFormat.setTimeZone(utc);
    Date convertedDate = sourceFormat.parse(dateStr);
    return destFormat.format(convertedDate);
}

原文由 J K 发布,翻译遵循 CC BY-SA 4.0 许可协议

tl;博士

Instant.parse( "2018-05-23T23:18:31.000Z" )                // Parse this String in standard ISO 8601 format as a `Instant`, a point on the timeline in UTC. The `Z` means UTC.
.atOffset( ZoneOffset.UTC )                                // Change from `Instant` to the more flexible `OffsetDateTime`.
.format(                                                   // Generate a String representing the value of this `OffsetDateTime` object.
    DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss" )   // Specify a formatting pattern as desired.
)                                                          // Returns a `String` object.

2018-05-23 23:18:31

国际标准化组织 8601

您的输入字符串采用标准 ISO 8601 格式。

java.time 类在解析/生成字符串时默认使用这些标准格式。

T 将年-月-日部分与时-分-秒分开。 Z 发音为 Zulu 并且表示 UTC

java.time

您正在使用多年前被 java.time 类取代的麻烦的旧日期时间类。 Apache DateUtils 也不再需要,因为您也会在 java.time 中找到它的功能。

将该输入字符串解析为 Instant 对象。 Instant 类表示 UTC 时间轴上的一个时刻,分辨率为 纳秒(最多九 (9) 位小数)。

 String input = "2018-05-23T23:18:31.000Z" ;
Instant instant = Instant.parse( input ) ;

要生成另一种格式的字符串,我们需要一个更灵活的对象。 Instant 类是一个基本的构建块。让 s convert it to a OffsetDateTime`,使用 UTC 本身作为指定的距 UTC 的偏移量。

 OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;

定义格式化模式以匹配所需的输出。

 DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss" ) ;
String output = odt.format( f ) ;

提示:考虑使用 DateTimeFormatter::ofLocalized… 方法自动本地化每个字符串生成 Locale 而不是硬编码格式化模式。


关于 java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 中搜索许多示例和解释。规范是 JSR 310

现在处于 维护模式Joda-Time 项目建议迁移到 java.time 类。

您可以直接与数据库交换 java.time 对象。使用符合 JDBC 4.2 或更高版本的 JDBC 驱动程序。不需要字符串,不需要 java.sql.* 类。 Hibernate 5 & JPA 2.2 支持 java.time

在哪里获取 java.time 类?

原文由 Basil Bourque 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题