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

标签 java java-7 utc datetime-parsing parseexception

我正在使用 Java 1.7。

尝试转换:

2018-05-23T23:18:31.000Z 

进入

2018-05-23 23:18:31

DateUtils 类:

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)?

最佳答案

tl;dr

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

ISO 8601

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

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

T 将年-月-日部分与时-分-秒分开。 Z 发音为 Zulu,意思是 UTC .

java.time

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

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

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

要生成另一种格式的字符串,我们需要一个更灵活的对象。 Instant 类是一个基本的构建 block 。让将其转换为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 及更高版本中。这些类取代了麻烦的旧类 legacy日期时间类,例如 java.util.Date , Calendar , & SimpleDateFormat .

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

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

您可以直接与您的数据库交换java.time 对象。使用JDBC driver符合 JDBC 4.2或以后。不需要字符串,不需要 java.sql.* 类。 Hibernate 5 和 JPA 2.2 支持 java.time

从哪里获取 java.time 类?

关于java - 如何转换 UTC 日期字符串并删除 Java 中的 T 和 Z?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50498949/

相关文章:

eclipse - 在eclipse(sts)中更改为jdk 7

python - 给定 UTC 时间戳和 UTC 偏移量,是否可以在 Python 中获取时区?

time - Lua UTC 时间不一致

java - 从 JDK 访问非公共(public)(java 原生)类 (7)

java - 在多个数组上获取相同的元素值

python - SQLAlchemy Date Time 对象可以在 Flask 中显示本地时区吗

java - JPA-onetomany级联在子表中插入相同的记录两次

java - 如何在 Maven 中配置 Checkstyle?

java - 错误: application requires permission

java - 如何将 application.properties 加载到测试类中以获取 spring.profiles.active 值?