-1

I have used the below code to convert the string to the required format. But in the output seconds, microseconds are missing.

LocalDate date = LocalDat.parse(inputString, DateTimeFormatter.ISO_LOCAL_DATE);
date.atStartOfDay(ZooneId.of("America/New_York").toOffsetDateTime().toString();

Output based on the above code: '2021-01-13T00:00-4:00'

Required output: '2020-01-13T00:00:00.000-4:00'

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
ssg
  • 69
  • 7
  • 4
    How can you loose something you don't have? If input is only a date then there is no time part to loose. – Joakim Danielson May 17 '21 at 12:15
  • 2
    Does this answer your question ? https://stackoverflow.com/questions/50120213/string-to-zoneddatetime-is-changing-format – Youcef LAIDANI May 17 '21 at 12:16
  • 3
    @Korashen Be careful with your use of capitalization. A "date object" might contain a date but if we're talking about a Date object, the common understanding will be a `java.util.Date`, and that *doesn't* only contain a date, despite its name. – Michael May 17 '21 at 12:17
  • 3
    Well, you're calling the `toString()` method, which returns *some* date-and-time format, which happens to be "2021-01-13T00:00-4:00". If you want to format your date, use, well, `format()`. – MC Emperor May 17 '21 at 12:19
  • 1
    @Korashen I agree with Michael here. While in German it may be normal to capitalize nouns, in English it's not. So *if* they are capitalized nevertheless, they could be interpreted as class names. (I assumed you speak German because your profile says you live in Germany. I also assumed that you capitalized Date because of that habit. Otherwise it's coincidence :-o) – MC Emperor May 17 '21 at 12:21
  • @MCEmperor Thank you for the comment. I was able to solve the issue based on your comment. – ssg May 17 '21 at 12:25
  • Yes, I am German and my native tongue came through. Sorry for not taking care. I deleted my comment to avoid confusion. As the issue seemd to be resolved by now, I just keep it deleted. – Korashen May 17 '21 at 12:27
  • 1
    It’s a funny format you are asking for. It’s almost [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC), only in ISO 8601 the offset must be given with 2-digit hours, for example `-04`, `-0400` or `-04:00`. Do you know for a fact that the consumer of your formatted string does not accept (strict) ISO 8601? Also asking because according to ISO 8601 the seconds and fraction of second are optional when they are 0. – Ole V.V. May 17 '21 at 12:49

1 Answers1

2

You can further reformat it using the format() method.

LocalDate date = LocalDate.parse("2021-01-13", DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(date.atStartOfDay(ZoneId.of("America/New_York")).toOffsetDateTime().format(DateTimeFormatter.ofPattern(("yyyy-MM-dd'T'HH:mm:ss:SSSZ"))));

see full list of available date formats here https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Ruelos Joel
  • 2,209
  • 3
  • 19
  • 33