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 ?
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