Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

How to convert Java String "EEE MMM dd HH:mm:ss zzz yyyy"date type to Java util.Date "yyyy-MM-dd"

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Table of all date-time types in Java, both modern and legacy

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...