c-an
c-an

Reputation: 4090

What's the time format of now() in Threeten?

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?

Upvotes: 0

Views: 772

Answers (1)

Sabuhi Talibli
Sabuhi Talibli

Reputation: 76

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

Upvotes: 6

Related Questions