简体   繁体   中英

extract date to string from a json date string

Android programming. I have a json returning the date from a database as a string. How do I extract the date from the string using Joda or any other library to this format mm/dd/yy. Here is the string 2012-05-22T00:00:00

It's ISO8601 date format so you can go with jodas:

DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
DateTime dt = fmt.parseDateTime(strInputDateTime);

And after that just format the DateTime object:

DateTimeFormatter fmt2 = DateTimeFormat.forPattern("MM/dd/yyyy");
String myDate = fmt2.print(dt)

This info is from http://joda-time.sourceforge.net/userguide.html#Standard_Formatters

With standard JDK, you could use SimpleDateFormat

Date parsedDateInstance = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(dateString);

String formattedDate = new SimpleDateFormat("MM/dd/yyyy").format(dateInstance);

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