I am using java with MongoDB to store some info including DATE using jdatechooser and I am trying to set the date in another page from the Data base.
When I look in the Db shell i find the date in this format(2019-08-04T17:40:04.022Z) ... I retrieve the DBObject and Set toString() then Print it to find it (Sun Aug 04 19:40:04 EET 2019).
I tested so many codes even from other answers but it seems to be all wrong formats for the date formatter.
However I always get this error :
java.text.ParseException: Unparseable date: "Sun Aug 04 19:40:04 EET 2019"
SimpleDateFormat sdf = new SimpleDateFormat("EE MM dd HH:mm:ss Z yyy");
Date date1 = sdf.parse(date);
System.out.println(date); //Prints Tue Oct 15 10:20:56 SGT 2015
jDateChooser1.setDate(date1);
Examples of the formats I used
yyyy-MM-dd HH:mm:ss.SSS Z
EE MM dd HH:mm:ss Z yyy
yyyy-MM-dd
dd-MM-yyyy
What are your suggestions to solve this issue ?? Please feel free to ask for question edits to help clarify the question
Thanks in advance
Apparently you are using some library you've not explained, jdatechooser
, to produce a java.util.Date
which is being stored somewhere along your tool chain as a string 2019-08-04T17:40:04.022Z
and in another part of your toolchain is being represented by the string Sun Aug 04 19:40:04 EET 2019
.
Let's tear those pieces apart.
We cannot help you with jdatechooser
until you name the library, and preferably link to its product site.
The java.util.Date
class is terrible, and is now legacy. Never use it. This class, along with SimpleDateFormat
& Calendar
, were supplanted years ago by the modern java.time classes with the adoption of JSR 310.
Specifically, Date
was replaced by java.time.Instant
. If you must interoperate with old code not yet updated to java.time, convert to-and-fro by calling new conversion methods added to the old class.
The string 2019-08-04T17:40:04.022Z
is in standard ISO 8601 format. The java.time classes use the standard formats by default when parsing/generating strings.
Instant instant = Instant.parse( "2019-08-04T17:40:04.022Z" ) ;
Generating.
String output = instant.toString() ; // Yields: 2019-08-04T17:40:04.022Z
The string Sun Aug 04 19:40:04 EET 2019
is in a terrible format. It assumes English, is difficult to parse by machine, fails to include all info (the fractional second is missing), and uses a pseudo-time-zone EET
rather than a true time zone such as Africa/Cairo
. Never use this format. Unfortunately, this is the format used by the java.util.Date::toString
method. Even worse, that toString
method dynamically applies the JVM’s current default time zone to adjust the value that is actually in UTC, creating much confusion. Again, never use Date
class for this and many other reasons.
You should focus on using Instant
whenever you can in your code. This class represents a moment in UTC.
When using JDBC, your driver may not support Instant
. If not, use OffsetDateTime
.
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
myPreparedStatement.setObject( … , odt ) ;
Retrieval.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;