0

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?

2
  • I recommend you don’t use SimpleDateFormat, TimeZone and Date. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead just use LocalDateTime from java.time, the modern Java date and time API. Commented Jan 3, 2020 at 22:28
  • Eh? It already is a java.sql.Timestamp. Don't convert it to String, and don't convert it back. Use rs.getTimestamp(). Commented Jan 3, 2020 at 23:24

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

yep...that did it! THANKS

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.