0

I have a string with date, that i want to convert to Date/LocalDateTime/Instant and to save it in PostgreSQL in future. How can I do it? Cases that i've tried:

String actualDate = "05-01-2020 03:07";
String pattern = "dd-MM-yyyy HH:mm";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime dateTime = LocalDateTime.parse(actualDate, formatter);
System.out.println(dateTime);

retunrs this value: 2020-01-05T03:07,

String actualDate = "05-01-2020 03:07";
String pattern = "dd-MM-yyyy HH:mm";
Date localDateTime = new SimpleDateFormat(pattern).parse(actualDate);
System.out.println(localDateTime);

returns Sun Jul 05 03:00:00 MSK 2020

Alexey
  • 362
  • 1
  • 5
  • 17
  • 2
    Do not longer use Date and SimpleDateFormat. – Jens Mar 29 '20 at 08:13
  • It is not really clear what your Problem is. – Jens Mar 29 '20 at 08:15
  • Problem is that string isn't saved in format that i need – Alexey Mar 29 '20 at 08:22
  • 3
    Ist will be saved as date type it has no format. What you See is the result of a toString function. – Jens Mar 29 '20 at 08:24
  • Is that means that i have to save in any format and parse after getting from database? – Alexey Mar 29 '20 at 08:28
  • You can use java.sql.Timestamp. Use `Timestamp.valueOf(localDateTime)` for conversion – Eklavya Mar 29 '20 at 08:44
  • 1
    @Alexey yes because or saves a date not a Formated string. – Jens Mar 29 '20 at 09:08
  • 2
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead depending on the type in your database use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 29 '20 at 09:42
  • What is the datatype in SQL? And which database engine are you using? When transferring a date-time object like `LocalDateTime` to and from your database, don’t worry about the format, your database driver will take care of that. – Ole V.V. Mar 29 '20 at 09:44
  • 1
    @AbinashGhosh That wold have been a good suggestion some years back. However since JDBC 4.2 we have nothing to use the poorly designed and long outdated `java.sql.Timestamp` for. We can save `LocalDateTime` and other modern types directly to the database. See for example [my answer here](https://stackoverflow.com/a/54907501/5772882). – Ole V.V. Mar 29 '20 at 09:46

2 Answers2

1

java.util.Date::toString tells a lie

The Date class represents a moment in UTC, having an offset of zero hours-minutes-seconds.

The toString method unfortunately applies the JVM’s current default time zone while generating the text. This creates the illusion of that zone being a part of that object.

Never use java.util.Date.

ISO 8601

Educate the publisher of your input data about the date-time formats defined in the ISO 8601 standard. These formats are designed for data exchange, easy to parse by machine, and easy to read by humans across cultures.

Database

Your particular input lacks a time zone or offset-from-UTC. So it should be stored in a column of a type akin to the standard SQL type of TIMESTAMP WITHOUT TIME ZONE (notice WITHOUT, not WITH). Postgres offers that type by that name.

Use a JDBC driver that complies with JDBC 4.2 or later. That standard requires support for exchanging date-time values using the modern java.time classes.

Parse input as shown in your Question.

String input = "05-01-2020 03:07";
String pattern = "dd-MM-yyyy HH:mm";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( pattern ) ;
LocalDateTime ldt = LocalDateTime.parse( input , formatter ) ;

Save to the database.

myPreparedStatement.setObject( … , ldt ) ;

Retrieve.

LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ; 
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1
String actualDate = "05-01-2020 03:07";

String pattern = "dd-MM-yyyy HH:mm";

Date localDateTime = new SimpleDateFormat(pattern).parse(actualDate);

System.out.println(new SimpleDateFormat(pattern).format(localDateTime));

Output : 05-01-2020 03:07

Hope this will work.

Birju B
  • 140
  • 1
  • 5
  • 2
    These terrible date-time classes are now legacy, supplanted years ago by the modern *java.time* classes defined in JSR 310. – Basil Bourque Mar 29 '20 at 16:15