0

I use ThreeTen module and when I print ZonedDateTime.now(), I get.

2019-07-11T22:43:36.564-07:00[America/Los_Angeles]

What's the format of this? I tried uuuu-MM-dd'T'HH:mm:ss.SSS'Z' and It says,

org.threeten.bp.format.DateTimeParseException: Text '2019-07-11T22:43:36.564-07:00[America/Los_Angeles]' could not be parsed at index 23

So, after SSS, the 'Z' part is incorrect.

What's the proper way to implement it?

This is my code:

val pstTime = ZonedDateTime.now(ZoneId.of("America/Los_Angeles")).toString()
val timeFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS'Z'")
val mTime = LocalDateTime.parse(pstTime, timeFormatter).toString()
tv_pstTime.text = mTime

I want to parse it to the format like Tuesday, July 2 5:15:01 P.M. How can I do that?

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
c-an
  • 2,036
  • 1
  • 17
  • 46
  • uuuu-MM-dd or yyyy-MM-dd? – Sony Jul 12 '19 at 06:29
  • you can see the `[` and `]` characters in the string. I think you need to add them to your format. Something like this: `yyyy-MM-dd'T'HH:mm:ss.SSS['Z']` – Vladyslav Matviienko Jul 12 '19 at 06:30
  • 1
    @Sony I was told that `uuuu` is better way. – c-an Jul 12 '19 at 06:57
  • @Sony [`uuuu` versus `yyyy` in `DateTimeFormatter` formatting pattern codes in Java?](https://stackoverflow.com/questions/41177442/uuuu-versus-yyyy-in-datetimeformatter-formatting-pattern-codes-in-java) – Ole V.V. Jul 12 '19 at 12:13
  • The quotes around `Z` would mean that there should be a literal `Z` in the date-time string. **Never** use this, it’s important. (Had there been a `Z` in your string, it would have been an offset, and you would have needed to parse it as such, not as a literal.) – Ole V.V. Jul 12 '19 at 12:24

1 Answers1

4

You can use DateTimeFormatter.ofPattern("...."). Inside .ofPattern("....") you can have any pattern you want.

Like this:

val result = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"))
                          .format(DateTimeFormatter.ofPattern("EEEE, MMMM d HH:mm:ss a"))

Output: Thursday, July 11 23:51:21 PM

  • You’ve looked through the questioner’s concrete questions and nicely spotted what s/he really needed. Truly great for a first Stack Overflow answer. Thanks and welcome here! – Ole V.V. Jul 12 '19 at 12:38