2

Can anybody let me know,how we can convert date in yyyy-MM-dd-HH.mm.ss.SSSSSS format to date in yyyy-MM-dd'T'HH:mm:ss.SSSz in Java,where in both input and output dates should be Strings.

I have used apache DateFormatUtils but that does not give milliseconds in the output.

Balwinder Singh
  • 2,272
  • 5
  • 23
  • 34
javaguy
  • 927
  • 2
  • 16
  • 37
  • 6
    http://stackoverflow.com/questions/1459656/how-to-get-the-current-time-in-yyyy-mm-dd-hhmisec-millisecond-format-in-java – chenchuk Jan 18 '16 at 22:53
  • 4
    [java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy](http://stackoverflow.com/q/18480633) ... just replaces the formats and do more research next time. – Tom Jan 18 '16 at 22:55
  • String date = "2016-01-12-17.04.14.470000"; String output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:sssZ").format(date); System.out.println(output); This gives me Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date – javaguy Jan 18 '16 at 23:02
  • @chenchuck and Tom - This Question asks about six digits of fractional second (microseconds) which raises a extra level of complexity over the possible duplicate Questions you linked. I would say this is not a dup. – Basil Bourque Jan 19 '16 at 00:30

3 Answers3

8

Java 8+

You could also use the newer Time API in Java 8, something like...

String formatIn = "yyyy-MM-dd-HH.mm.ss.SSSSSS";
String formatOut = "yyyy-MM-dd'T'HH:mm:ss.SSSz";

String valueIn = "2016-01-19-09.55.00.000000";

LocalDateTime ldt = LocalDateTime.parse(valueIn, DateTimeFormatter.ofPattern(formatIn));
System.out.println("< " + ldt);

ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());        
String out = DateTimeFormatter.ofPattern(formatOut).format(zdt);
System.out.println("> " + out);

Which outputs...

< 2016-01-19T09:55
> 2016-01-19T09:55:00.000AEDT

This makes you far more responsible for managing the time zones which might be a better solution generally

And because converting between time zones in the Java 8 API gives me a headache (lack of experience :P)

LocalDateTime ldt = LocalDateTime.parse(valueIn, DateTimeFormatter.ofPattern(formatIn));
System.out.println("< " + ldt);

ZonedDateTime here = ldt.atZone(ZoneId.systemDefault());
System.out.println("here " + here);

ZonedDateTime there = here.withZoneSameInstant(ZoneId.of("GMT"));
System.out.println("there " + there);
String out = DateTimeFormatter.ofPattern(formatOut).format(there);
System.out.println("> " + out);

Which outputs...

< 2016-01-19T09:55
here 2016-01-19T09:55+11:00[Australia/Sydney]
there 2016-01-18T22:55Z[GMT]
> 2016-01-18T22:55:00.000GMT

FYI: I think your input is using nano/micro seconds and not milliseconds (there's only 1000 milliseconds in a second). SimpleDateFormat does not support nano/micro seconds, but DateTimeFormatter does, you'd have to use the n pattern, yyyy-MM-dd-HH.mm.ss.nnnnnn for example

Java 7 and below

The basic answer is, use a SimpleDateFormat....

String formatIn = "yyyy-MM-dd-HH.mm.ss.SSSSSS";
String formatOut = "yyyy-MM-dd'T'HH:mm:ss.SSSz";

String valueIn = "2016-01-19-09.55.00.000000";

SimpleDateFormat in = new SimpleDateFormat(formatIn);
SimpleDateFormat out = new SimpleDateFormat(formatOut);

Date dateIn = in.parse(valueIn);
System.out.println("< " + dateIn);

String valueOut = out.format(dateIn);
System.out.println("> " + valueOut);

Which outputs...

< Tue Jan 19 09:55:00 AEDT 2016
> 2016-01-19T09:55:00.000AEDT

The problem here is, you could be converting across different time zones, which case, you could use something like...

in.setTimeZone(TimeZone.getTimeZone("GMT"));
dateIn = in.parse(valueIn);
System.out.println("< " + dateIn);

out.setTimeZone(TimeZone.getTimeZone("GMT"));
valueOut = out.format(dateIn);
System.out.println("> " + valueOut);

which outputs

< Tue Jan 19 20:55:00 AEDT 2016
> 2016-01-19T09:55:00.000GMT

or a combination of, if you want to covert to a different time zone.

But, personally, I'd use Joda-Time, but that's me

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Many Thanks for your detailed answer.If I want time in UTC,is this right to use...... in.setTimeZone(TimeZone.getTimeZone("UTC")); out.setTimeZone(TimeZone.getTimeZone("UTC")); – javaguy Jan 18 '16 at 23:27
  • AFAIK, yes. You might want to read the documentation for `DateFormat`, but that's the gist – MadProgrammer Jan 18 '16 at 23:29
  • Important to note that the old java.util.Date cannot handle those six digits in the fraction of second (microsecond). That class can handle only three digits, for millisecond resolution. The [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes are a *vast* improvement, supporting nine digits of fractional second (nanosecond). Use these, as suggested in the latter half of this Answer. Avoid the old classes wherever possible. – Basil Bourque Jan 19 '16 at 00:21
2
        SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS");
        SimpleDateFormat s2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
        Date d= s1.parse( "2015-11-11-02.01.11.000001" );
        String str= s2.format( d);
        System.out.println(str);

This parse the string date to a Date project using the SimpleDateFormat s1. Then it parse it to the desired form using a second SimpleDateFormat.

sguan
  • 1,039
  • 10
  • 26
  • Many Thanks.This is almost what I was looking for.The only issue is when in input milliseconds are greater than 999,time is getting added in minutes to the original minutes.Can we avoid this .Say if I have input as 2016-01-12-17.04.14.470000,can we get output like 2016-01-12T17:04:14.470+01:00 – javaguy Jan 18 '16 at 23:18
  • 1
    @user2603985 Short answer is no, there are only 1000 milliseconds in a second. I think you time is using nano seconds, which the `SimpleDateFormat` doesn't handle – MadProgrammer Jan 18 '16 at 23:19
-2
String foramt = "yyyy-MM-dd HH:mm:ss.SSS";
String dbdate = "2021-05-19 13:13:20.247";
LocalDateTime ldt = LocalDateTime.parse(dbdate, DateTimeFormatter.ofPattern(foramt));
ZonedDateTime finalDateFormat = ldt.atZone(ZoneId.of("UTC")).withZoneSameInstant(ZoneId.of("UTC"));
System.out.println(finalDateFormat.toOffsetDateTime());
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
Ramya Ravi
  • 21
  • 2