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
7
  • When you’ve got a date without time of day, what sense does it make to require it in a particular time zone? Commented Jun 10, 2019 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);? Commented Jun 10, 2019 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). Commented Jun 10, 2019 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 (at first sight it seems to me you don’t need any other classes). Commented Jun 10, 2019 at 14:17
  • 1
    @P.Soutzikevich No, not a duplicate. That Question is about a date with time, whereas this Question appears to be about a date-only (though unclear). Commented Jun 10, 2019 at 20:55

2 Answers 2

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 ) ;
Sign up to request clarification or add additional context in comments.

Comments

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.