Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How do I format this date

Posted on November 11, 2022 By

Solution 1 :

tl;dr

OffsetDateTime
.parse( 
    "2021-01-15T19:00:00+0000" , 
    DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ssxxxx" ) 
)
.toString()

2021-01-15T19:00Z

OffsetDateTime

Your input has an offset of zero hours-minutes-seconds from UTC, the +0000 portion.

So you should be parsing with OffsetDateTime rather than ZonedDateTime.

ZonedDateTime

The ZonedDateTime class is for a time zone rather than an offset. A time zone is the named history of the past, present, and future changes to the offset used by the people of a particular region, as decided by their politicians. Time zones have a name in format of Continent/Region such as Europe/Paris or America/Edmonton.

Optional COLON in offset

Unfortunately, your input omitted the COLON character from between the hours and minutes of the offset. While optional in the ISO 8601 standard, the parse method expects to find that character.

If you know all the inputs have the same +0000 I would simply perform a string manipulation.

OffsetDateTime odt = OffsetDateTime.parse( "2021-01-15T19:00:00+0000".replace( "+0000" , "+00:00" ) ) ;

If some other offsets might appear, you must specify a formatting pattern.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ssxxxx" ) ;
OffsetDateTime odt = OffsetDateTime.parse( input , f ) ;

See this code run live at Ideone.com.

2021-01-15T19:00Z

Solution 2 :

I would recommend to use the OffsetDateTime with the right offset – that should do the trick!

Problem :

This question already has answers here:

Why can’t OffsetDateTime parse ‘2016-08-24T18:38:05.507+0000’ in Java 8

(5 answers)
Closed 5 months ago.

I am trying to parse the date which is in the format 2021-01-15T19:00:00+0000. I am not sure which format it is but based on my research I tried with methods below

1. val odt = OffsetDateTime.parse("2021-01-15T19:00:00+0000")
2. val zdt = ZonedDateTime.parse("2021-01-15T19:00:00+0000")

It is logging the exception:

java.time.format.DateTimeParseException: Text '2021-01-15T19:00:00+0000' could not be parsed at index 19
        at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
        at java.time.ZonedDateTime.parse(ZonedDateTime.java:591)
        at java.time.ZonedDateTime.parse(ZonedDateTime.java:576)

Comments

Comment posted by ISO 8601

I agree with you that

READ  [FIXED] android - Firebase storage no internet connection callback
Powered by Inline Related Posts

Android Tags:android, date, java-time, kotlin, timezone-offset

Post navigation

Previous Post: [FIXED] android – Overlapping items in RelativeLayout with ImageView and TextView
Next Post: [FIXED] android – Unable to instantiate application com.company.app.MainApplication: java.lang.ClassNotFoundException React Native Expo

Related Posts

[FIXED] android – No Data in Recycler View in Fragment Android
[FIXED] java – How to update the recycler without changing the list Android
[FIXED] java – NullPointerException Error without line number. How can I get the line number that is causing this error in Android Studio? Android
[FIXED] android – Why views not showing in one of the jetpack compose screen? Android
[FIXED] Android Motion Layout – Observe End of transition Android
[FIXED] retrofit2 – POSTMAN works, but android okhttp request responds different value Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme