Gloria Santin
Gloria Santin

Reputation: 2136

Oracle Timestamp value is not converting to the java datetime

I have a query for an Oracle database that returns a datetime column. In the java method, the column is converted to a string. A portion of the code looks like this:

ResultSet rs;
HashMap<String, String> hm=new HashMap<String, String> ();
 hm.put("SchEndDate2", rs1.getString("END_DT_TM_GMT"));

When I view the strings value in the debugger it looks like this: "2019-07-04 11:00:00.0" I need to convert this string to the datetime format of this: "yyyy-MM-dd'T'HH:mm"

I tried this SimpleDateFormat to complete this but when I convert the string to the format it returns the dateTime in Eastern Daylight Time and not GMT. The value after going thru the conversion is this: "Thu Jul 04 07:00:00 EDT 2019" This is the code that I am using to convert the string to a DateTime.

  EndDate=map.get("SchEndDate2");
//EndDate : **"2019-07-04 11:00:00.0"**
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date databaseDateTime = formatter.parse(EndDate);
//databaseDateTime: **"Thu Jul 04 07:00:00 EDT 2019"**

Why is the format incorrect and the timezone not correctly set?

Upvotes: 0

Views: 1657

Answers (2)

Anonymous
Anonymous

Reputation: 86399

Two points.

  1. Don’t fetch your date and time as a string from Oracle. Fetch a proper date-time object. In this case a LocalDateTime.
  2. Don’t use SimpleDateFormat, TimeZone and Date. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Use java.time, the modern Java date and time API.

In code:

    ResultSet rs = // …;
    LocalDateTime dateTime = rs.getObject("END_DT_TM_GMT", LocalDateTime.class);

    String databaseDateTimeAsString = dateTime.toString();

    System.out.println(databaseDateTimeAsString);

Example output:

2019-07-04T11:00

It’s not quite the output format that you asked for, but it most likely will serve your purpose. The format you asked for is ISO 8601. So is the output I have given you. In the ISO 8601 standard, including the seconds and fraction of second when they are 0 is optional. If you insist on including them, use a formatter. For example:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS");
    String databaseDateTimeAsString = dateTime.format(formatter);

2019-07-04T11:00:00.000

Using rs.getObject() for getting a LocalDateTime as shown requires a JDBC 4.2 compliant database driver. You probably have got that. In case you haven’t and you cannot upgrade, use:

    LocalDateTime dateTime = rs.getTimestamp("END_DT_TM_GMT").toLocalDateTime();

Links

Upvotes: 1

Kaan
Kaan

Reputation: 5794

Try converting the Date object to an Instant. Here's an example showing your input string first converted to a Date, and then converting that to an Instant. The date contains the timezone-specific rendering, but the instant does not.

String input = "2019-07-04 11:00:00.0";
System.out.println("input:   " + input);

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = formatter.parse(input);
System.out.println("date:    " + date);

Instant instant = date.toInstant();
System.out.println("instant: " + instant);

And here's the output:

input:   2019-07-04 11:00:00.0
date:    Thu Jul 04 05:00:00 MDT 2019
instant: 2019-07-04T11:00:00Z

Upvotes: 0

Related Questions