简体   繁体   中英

How to format this incoming timestamp String from server?

I'm receiving this Timestamp string from the server:

"timestamp": "2021-02-21T22:15:37.672+00:00" I'm not sure what format it's in

how I can format it to a Month day time format such as February 21st, 4:00pm ?

Edit:

Using the answer below:

String time = "2021-02-21T22:15:37.672+00:00";
try {
  Date dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse(time);
  Log.d(TAG, "onCreate: " + dateFormat.toString());
} catch (ParseException e) {
  e.printStackTrace();
}

That most likely is UTC time. You can tell it is because it doesn't have an offset via the +00:00 , which is used for timezone offset. UTC can be converted to other timeszones using DateFormat . Ref:

https://developer.android.com/reference/java/text/DateFormat

"2021-02-21T22:15:37.672+00:00" is "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" in SimpleDateFormat in JAVA SE7.

"February 21st, 4:00pm" is "MMM dd, h:mma"
The "st" part seems like it have to be determine manually in SimpleDateFormat.

SimpleDateFormat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM