How to convert Java String EEE MMM dd HH:mm:ss zzz yyyy date type to Java util.Date yyyy-MM-dd
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 SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 brought some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android (26+) bundle implementations of the java.time classes.
- For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
- If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
How to compare the datetime of format EEE MMM dd HH:mm:ss zzz yyyy and yyyy-MM-dd hh:mm:sss in java?
If you assume that the timezone of the second date is the same as for the first one then you can just use java.time. It has all parsing tools you need. Any other fixed timezone works as well.
Here is an example:
String a = "Wed Mar 04 03:34:45 GMT+08:00 2020";
String b = "2020-02-04 02:10:58";
ZonedDateTime parsedA;
ZonedDateTime parsedB;
DateTimeFormatter formatterA = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
parsedA = ZonedDateTime.parse(a, formatterA);
DateTimeFormatter formatterB = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
parsedB = LocalDateTime.parse(b, formatterB).atZone(parsedA.getZone());
// What do you want to compare? For example you can tell if a is after b.
System.out.println(parsedA.isAfter(parsedB));
Have a look here if you need another format and need a listing of Pattern Letters and Symbols.
How to parse date to EEE MMM dd HH:mm:ss zzz yyyy format?
Your SimpleDateFormat
pattern does not match createdDate
. The format should be:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
See the javadoc
Date conversion in MM/DD/YY h:mm:ss a format in java
As per the JavaDoc
, D
stands for Day in year. You will need to replace DD
with dd
(day in month).
Y
stands for Week year, not year.
In short, this: "DD-MMM-YYYY"
needs to be this: "dd-MMM-yyyy"
.
Trouble formatting date in Java
When debugging parsing issues, if possible, reverse the operation and generate the text you're supposed to be parsing, to verify the parsing rules, i.e. the date format string. This applies to date parsing, JAXB parsing, and any other (de)serializing operation that is bi-directional. It makes finding conversion rule issues a lot easier.
So, let us check the format string in the question, with the shown date value:
ZonedDateTime dateTime = ZonedDateTime.of(2020, 12, 28, 15, 18, 16, 0, ZoneOffset.UTC);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss Z YYYY", Locale.US);
System.out.println(dateTime.format(fmt));
Output
Mon Dec 28 15:18:16 +0000 2021
Oops! That doesn't fit the expected output, aka the input we desire to parse:
Mon Dec 28 15:18:16 UTC 2020
So what went wrong?
The year is wrong because it's supposed to be
uuuu
(year), notYYYY
(week-based-year).The time zone is wrong because
Z
does support a text representation. UseVV
orz
instead.
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu", Locale.US);
ZonedDateTime dateTime = ZonedDateTime.parse("Mon Dec 28 15:18:16 UTC 2020", fmt);
System.out.println(dateTime);
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS")));
Output
2020-12-28T15:18:16Z[UTC]
2020-12-28 15:18:16.000
As you can see, it now parsed correctly.
Related Topics
Why Can't Static Methods Be Abstract in Java
Why Does Arrayindexoutofboundsexception Occur and How to Avoid It in Android
Problems with Java3D Lib Configuration
Java -Xbootclass Path Is No Longer a Supported Option
Wireless API for Linux in C or Java
Why Is Alternatives Command Used When Installing Java on a Linux MAChine
Eclipse Swt Browser Crash (Linux 64Bit)
Eclipse 3.6 Helios for Ubuntu 10.10
Best Way to Convert an Arraylist to a String
How to Parse a Local JSON File from Assets Folder into a Listview
Image Encryption/Decryption Using Aes256 Symmetric Block Ciphers
Add Custom Headers to Webview Resource Requests - Android
Java Is Installed, in Listing, But Execution Produces "./Java: No Such File or Directory"
Verify If a Process Is Running Using Its Pid in Java
Benefits/Drawbacks to Running 64-Bit Jvm on 64-Bit Linux Server