简体   繁体   English

从json日期字符串中提取日期到字符串

[英]extract date to string from a json date string

Android programming. Android程式设计。 I have a json returning the date from a database as a string. 我有一个json从数据库返回日期作为字符串。 How do I extract the date from the string using Joda or any other library to this format mm/dd/yy. 如何使用Joda或任何其他库从字符串中提取日期为这种格式mm / dd / yy。 Here is the string 2012-05-22T00:00:00 这是字符串2012-05-22T00:00:00

It's ISO8601 date format so you can go with jodas: 这是ISO8601日期格式,所以你可以去jodas:

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

And after that just format the DateTime object: 之后,只需格式化DateTime对象:

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 此信息来自http://joda-time.sourceforge.net/userguide.html#Standard_Formatters

With standard JDK, you could use SimpleDateFormat 使用标准JDK,您可以使用SimpleDateFormat

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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