0

I'm trying to convert a JSON object to a date and write it to a database. The code to convert the date to EST isn't working however. What's the best way to convert a date to EST?

JSON entry

"custom_date_1": "2019-05-19","

Conversion Code

int id;
Date trainingDate;

SimpleDateFormat format = new SimpleDateFormat("DD-MM-YY", Locale.US);
TimeZone tz = TimeZone.getTimeZone("EST");
format.setTimeZone(tz);

logger.info("custom_date_1: {}", object.getString("custom_date_1"));

 try {
      id= Integer.parseInt(object.getString("employee_number"));
      trainingDate = format.parse(object.getString("custom_date_1"));

      //Still says GMT
      logger.info("trainingDate: {}", trainingDate);

       map.put("employee_number", id);
       map.put("custom_date_1", trainingDate);
   } catch (ParseException e) {
       e.printStackTrace();
   }

Log Statement

2019-06-10 14:00:00,226 INFO custom_date_1: 2019-05-19
2019-06-10 14:00:00,226 INFO trainingDate: Sun Dec 30 05:00:00 GMT 2018
PT_C
  • 1,178
  • 5
  • 24
  • 57
  • When you’ve got a date without time of day, what sense does it make to require it in a particular time zone? – Ole V.V. Jun 10 '19 at 14:14
  • 1
    Any date is the same date in a specific timezone. So you could just convert it to date with `LocalDate.parse(date);`? – achAmháin Jun 10 '19 at 14:15
  • 2
    When you say "EST" do you really mean "Eastern Standard Time" (the UTC offset observed in the winter in New York), or do you mean "the time zone in New York"? If the former, express it as `UTC+5`; if the latter, express it as `America/New_York`. ("EST" actually is the former). – Andy Turner Jun 10 '19 at 14:16
  • 1
    I recommend you don’t use `Date`, `SimpleDateFormat` and `TimeZone`. Those classes are poorly designed and long outdated, the middle one in particular notoriously troublesome. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) (at first sight it seems to me you don’t need any other classes). – Ole V.V. Jun 10 '19 at 14:17
  • It’s a variant of [Parsing a string to date format in java defaults date to 1 and month to January](https://stackoverflow.com/questions/33427670/parsing-a-string-to-date-format-in-java-defaults-date-to-1-and-month-to-january). You’ve got a couple of other bugs in your code too, though, compared to that question. – Ole V.V. Jun 10 '19 at 14:20
  • Possible duplicate of [Convert string to Date in java](https://stackoverflow.com/questions/9945072/convert-string-to-date-in-java) – Soutzikevich Jun 10 '19 at 14:47
  • 1
    @P.Soutzikevich No, not a duplicate. [That Question](https://stackoverflow.com/questions/9945072/convert-string-to-date-in-java) is about a date with time, whereas this Question appears to be about a date-only (though unclear). – Basil Bourque Jun 10 '19 at 20:55

2 Answers2

2

tl;dr

myPreparedStatement.setObject(       // In JDBC 4.2 and later, exchange *java.time* objects with your database. Use `PreparedStatement` to avoid SQL Injection attacks. 
    … ,                              // Specify which `?` placeholder to replace in your SQL statement. 
    LocalDate.parse( "2019-05-19" )  // Parse your standard ISO 8601 formatted input string as a date-only value represented in the `LocalDate` class. 
) 

Details

"custom_date_1": "2019-05-19",

Your input string is in standard ISO 8601 format. These standard formats are used by default in the java.time classes when parsing/generating strings. So no need to specify a formatting pattern.

LocalDate ld = LocalDate.parse( "2019-05-19" ) ;

Time zone is irrelevant here. So your Question is quite confusing.

I'm trying to convert a JSON object to a date and write it to a database.

As of JDBC 4.2, we can exchange java.time objects with the database. Your column in the database for this date-only value (without time-of-day and without time zone) should be of a type akin to the standard SQL type DATE.

myPreparedStatement.setObject( … , ld ) ;

Retrieve.

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

From Java 8 onwards you should be using LocalDate and LocalDateTime, so you can do timezone conversions using

LocalDateTime.atZone(ZoneId zoneId)

For example

LocalDateTime date = LocalDateTime.parse("2019-06-10T12:00:00"); 
ZonedDateTime date2 = date.atZone(ZoneId.systemDefault());
steven35
  • 3,747
  • 3
  • 34
  • 48