1

I have a date that I want to change into a different format.

Current format: Fri Apr 07 08:21:19 MDT 2017 Desired format: 2017-04-07T13:28:41.00MDT. I want to have the desired format output as a string.

I am currently doing this:

DateTimeFormatter sourceFormat = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu");
ZonedDateTime dateParsed = ZonedDateTime.parse(dateStr, sourceFormat); 
String fullDateTimeStr = fullDateTime.format(dateParsed);

The formatting is correct, but the main issue I'm having is preserving the fractional seconds (the two numbers right before the time zone.) I know that the date I am receiving has the ms/fractional seconds preserved because when I print out long epoch = ((Date) date).getTime(); I get the epoch time and I can convert it (using an online conversion tool) and it shows the correct date/time with ms.

What's going on?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Vic
  • 99
  • 2
  • 11
  • I don't understand from where should the "fractional second" come? –  Apr 14 '17 at 18:08
  • The example timestamp "Fri Apr 07 08:21:19 MDT 2017" doesn't have any fractional seconds represented, so I wouldn't expect any to be parsed. I actually parsed that string, and got the milliseconds by doing: dateParsed.toInstant().toEpochMilli(); The result was 1491574879000. As expected, the result is divisible by 1000 with no remainder, which means it has no fractional seconds. – Code4aliving Apr 14 '17 at 18:20
  • While MDT is probably unambiguous (Mountain Daylight Time), many of the three or four letter abbreviations are ambiguous and best avoided. – Ole V.V. Apr 14 '17 at 19:11
  • Yes, the sourceFormat formatter is not preserving the ms. The original value that is being passed in is an instance of a Date. In order to format it I was using the DateTimeFormatter sourceFormat, but when I try the pattern `"EEE MMM dd HH:mm:ss.SS z uuuu"` I get a parser error. However, the original date does have ms, because when I call `getTime`, it returns a long. When I put that long into an online epoch converter, it gives me ms. – Vic Apr 14 '17 at 19:15
  • (A) So are you starting with a string or with a `Date` object — your Question contradicts your Comment. Edit your Question to be clear. (B) Are you asking for exactly two decimal places even if zeros? Or do you want any and all digits of a fractional second? Again, edit your Question. (C) Post your actual inputs and outputs including the mysterious epoch converter you mentioned. – Basil Bourque Apr 14 '17 at 20:08

1 Answers1

1
    DateTimeFormatter targetFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSz");
    ZonedDateTime zdt = ZonedDateTime.now();
    System.out.println(zdt.format(targetFormat));

Sample output:

2017-04-14T21:19:58.409CEST

You need SSS in the format pattern for the milliseconds.

Please consider whether you need the three or four letter time zone abbreviations that are often ambiguous. I would prefer to use DateTimeFormatter.ISO_ZONED_DATE_TIME for an output like 2017-04-14T21:22:49.344+02:00[Europe/Berlin]. This also saves you from building the pattern string yourself.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161