-1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Joseph Hwang
  • 1,337
  • 3
  • 38
  • 67
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `ZonedDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 24 '21 at 18:57
  • Given that you want that output, you may convert to `LocalDate`. It prints like `2020-12-12` (always). – Ole V.V. Jan 25 '21 at 02:42

1 Answers1

5

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Kindly inform me what is the pattern of this string value is "2020-12-18 16:04:07-06"? I use @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss:SSS". But It throws Exception. – Joseph Hwang Jan 23 '21 at 12:00
  • @JosephHwang Your formatting pattern is missing codes for the offset-from-UTC appearing at the end of your input. Parse that input as a `java.time.OffsetDateTime` object in Java. – Basil Bourque Jan 23 '21 at 17:59
  • @JosephHwang `OffsetDateTime.parse( "2020-12-18 16:04:07-06".replace( " " , "T" ) )` – Basil Bourque Jan 24 '21 at 02:56