-2

I'm very new to Java. I'm fetching this date 1995-01-28T17:02:12.936000-0500 from oracle db and now I want to convert it into yyyy-MM-dd HH:mm:ss format. I have tried below method

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
Date c=sdf.parse("1995-01-28T17:02:12.936000-0500");
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System.out.println(sdf1.format(c));

I'm getting below output

1995-2-28 17:17:48 You can see that 17:02:12 is getting changed to 17:17:48. How do I fix this ?

n0noob
  • 889
  • 8
  • 23
BEGINNER
  • 31
  • 6
  • I plant to use that date and time as a string – BEGINNER Jul 01 '21 at 19:12
  • 1
    `936000` ms is 15 minutes, pretty sure the difference comes from those – Aaron Jul 01 '21 at 19:21
  • I'm sorry If dont get this. But does this mean the total time is correct? – BEGINNER Jul 01 '21 at 19:22
  • 1
    I doubt it : I assume the 936000 by the end of the date you retrieve should be understood as 936 ms (and the last 3 zeroes would be for additional precision), but SimpleDateFormat doesn't have any format for anything more precise than milliseconds. See if you can change the format of the date you retrieve – Aaron Jul 01 '21 at 19:24
  • @Aaron ok I get it now why the extra minutes were added onto it. – BEGINNER Jul 01 '21 at 19:30
  • 1
    You are using terrible date-time classes that were supplanted years ago by the modern *java.time* classes defined in JSR 310. – Basil Bourque Jul 02 '21 at 03:16
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 02 '21 at 18:49

2 Answers2

3

Try it like this. But use the methods in the java.time package. It is superior in many ways to the java.util.Date and supported methods which are outmoded (and quite a few are deprecated).

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
OffsetDateTime odt = OffsetDateTime.parse("1995-01-28T17:02:12.936000-0500",dtf);
DateTimeFormatter resultFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(odt.format(resultFormat));

Prints

1995-01-28 17:02:12
WJS
  • 36,363
  • 4
  • 24
  • 39
  • No need for a DateTimeFormatter when parsing. [OffsetDateTime.parse(String)](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/time/OffsetDateTime.html#parse(java.lang.CharSequence)) will handle that format by default. – VGR Jul 01 '21 at 21:24
  • 1
    @VGR I tried it and it wouldn't work. Perhaps because there is no colon in the offset of the OP's timestamp. – WJS Jul 01 '21 at 22:19
  • 1
    @VGR The input data omits the optional COLON Character between the hours and minutes of the offset. The default parser expects that missing character. So parsing fails. – Basil Bourque Jul 02 '21 at 03:18
0

Remove the 000 from the 936000. The date parser treats that as 936,000 milliseconds, which is adding 936 seconds, or about 15 minutes, to your date/time.

eattrig
  • 326
  • 1
  • 4
  • 12