如何将Java字符串"EEE MMM dd HH:mm:ss zzz yyyy"日期类型转换为Java util.Date"yyyy-MM-dd" [英] How to convert Java String "EEE MMM dd HH:mm:ss zzz yyyy"date type to Java util.Date "yyyy-MM-dd"

查看:222
本文介绍了如何将Java字符串"EEE MMM dd HH:mm:ss zzz yyyy"日期类型转换为Java util.Date"yyyy-MM-dd"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的原始日期类型为 EEE MMM dd HH:mm:ss zzz yyyy .例如, Sat Dec 12 00:00:00 KST 2020 ,但是我的目标日期类型是Java日期类型,其格式为 yyyy-MM-dd .因此,我制作了将Java字符串类型 EEE MMM dd HH:mm:ss zzz yyyy 转换为Java util对象的方法.

 私有静态日期getDate(String beforeDate)引发异常{SimpleDateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.US);日期rdate = readFormat.parse(beforeDate);SimpleDateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd",Locale.US);字符串格式= writeFormat.format(rdate);System.out.println(format);返回writeFormat.parse(format);} 

System.out.println(format)行将打印出我期望的正确值.

  2020-12-122020-12-122013-01-01 

但是返回值的类型是错误的.该方法的返回值似乎不受影响.

  System.out.println(getDate("Sat Dec 12 00:00:00 KST 2020")); 

上面的行再次打印 Sat Dec 12 00:00:00 KST 2020 .我不知道在转换Java日期类型时哪些代码是错误的.任何建议将被认真考虑.最好的问候

解决方案

tl; dr

  ZonedDateTime.parse("2020年12月12日星期六00:00:00 KST",DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz uuuu").withLocale(Locale.US)).toLocalDate().toString() 

2020-12-12

避免使用旧的日期时间类

您使用的是可怕的日期时间类,而这些类早已被现代的 java.time 类取代.永远不要使用 Date Calendar SimpleDateFormat .

您说:

我的目标日期类型是Java日期类型,格式为yyyy-MM-dd.

显然,您希望 Date 保存日期.它不是. java.util.Date 类表示一个时刻,以UTC格式显示的带有日期的日期. java.sql.Date 类伪装为仅保留日期,但实际上也包含一个日期,如UTC所示.

java.util.Date 类的许多问题是其 toString 方法的行为.该方法在生成文本时动态地应用JVM的当前默认时区.这种反特征可能会加剧您的困惑.

LocalDate

相反,您应该使用 java.time.LocalDate 来表示仅日期的值,而没有日期,时间段或UTC偏移量.

ZonedDateTime

首先使用 DateTimeFormatter 类来解析您的整个输入字符串.这样会产生一个 ZonedDateTime 对象,该对象表示某个时刻,这一时刻是通过特定地区的人们使用的挂钟时间看到的.

示例代码

 字符串输入="Sat Dec 12 00:00:00 KST 2020";DateTimeFormatter f = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz uuuu").withLocale(Locale.US);ZonedDateTime zdt = ZonedDateTime.parse(input,f); 

从该 ZonedDateTime 对象中,提取 LocalDate .

  LocalDate localDate = zdt.toLocalDate(); 

请参见此

旧版日期时间类,例如 SimpleDateFormat .

要了解更多信息,请参见 Oracle教程.并在Stack Overflow中搜索许多示例和说明.规范为 JSR 310 .

Joda-Time 项目,现在位于<一个href ="https://en.wikipedia.org/wiki/Maintenance_mode" rel ="nofollow noreferrer">维护模式,建议迁移到 JDBC驱动程序/jeps/170"rel =" nofollow noreferrer> JDBC 4.2 或更高版本.不需要字符串,不需要 java.sql.* 类.休眠5&JPA 2.2支持 java.time .

在哪里获取java.time类?

  • Java SE 6 Java SE 7
  • Android
  • My source date type is EEE MMM dd HH:mm:ss zzz yyyy. For example, Sat Dec 12 00:00:00 KST 2020 But my target date type is Java Date type with format yyyy-MM-dd. So I make the method which convert Java String type EEE MMM dd HH:mm:ss zzz yyyy to java util object.

    private static Date getDate(String beforeDate) throws Exception{
            
            SimpleDateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            Date rdate = readFormat.parse(beforeDate);
            
            SimpleDateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US); 
            String format = writeFormat.format(rdate);
            
            System.out.println(format);
            
            return writeFormat.parse(format);
        }
    

    System.out.println(format) line prints the right values which I expect.

    2020-12-12
    2020-12-12
    2013-01-01
    

    But the type of return values is wrong. The return value from the method seems not to be influenced.

    System.out.println(getDate("Sat Dec 12 00:00:00 KST 2020")); 
    

    The above line prints Sat Dec 12 00:00:00 KST 2020 again. I have no idea which codes are wrong in converting Java date type. Any advice will be appreciated. Best regards

    解决方案

    tl;dr

    ZonedDateTime
    .parse( 
        "Sat Dec 12 00:00:00 KST 2020" ,
        DateTimeFormatter
        .ofPattern( "EEE MMM dd HH:mm:ss zzz uuuu" )
        .withLocale( Locale.US )
    )
    .toLocalDate()
    .toString() 
    

    2020-12-12

    Avoid legacy date-time classes

    You are using terrible date-time classes that were supplanted years ago by the modern java.time classes. Never use Date, Calendar, or SimpleDateFormat.

    You said:

    my target date type is Java Date type with format yyyy-MM-dd.

    Apparently, you expect Date to hold a date. It does not. The java.util.Date class represents a moment, a date with time-of-day as seen in UTC. The java.sql.Date class pretends to hold only a date, but it too actually contains a date with time-of-day as seen in UTC.

    Among the many problems with java.util.Date class is the behavior of its toString method. That method dynamically applies the JVM’s current default time zone while generating text. This anti-feature may contribute to your confusion.

    LocalDate

    Instead you should be using java.time.LocalDate to represent a date-only value without a time-of-day and without a time zone or offset-from-UTC.

    ZonedDateTime

    First use the DateTimeFormatter class to parse your entire input string. This results in a ZonedDateTime object representing a moment as seen through the wall-clock time used by the people of a particular region.

    Example code

    String input = "Sat Dec 12 00:00:00 KST 2020" ;
    DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss zzz uuuu" ).withLocale( Locale.US ) ;
    ZonedDateTime zdt = ZonedDateTime.parse( input , f ) ;
    

    From that ZonedDateTime object, extract a LocalDate.

    LocalDate localDate = zdt.toLocalDate() ;
    

    See this code run live at IdeOne.com.

    zdt.toString(): 2020-12-12T00:00+09:00[Asia/Seoul]

    ld.toString(): 2020-12-12


    About java.time

    The 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.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

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

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

    Where to obtain the java.time classes?

    这篇关于如何将Java字符串"EEE MMM dd HH:mm:ss zzz yyyy"日期类型转换为Java util.Date"yyyy-MM-dd"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆