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?
SimpleDateFormat,TimeZoneandDate. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead just useLocalDateTimefrom java.time, the modern Java date and time API.java.sql.Timestamp. Don't convert it to String, and don't convert it back. Users.getTimestamp().