【发布时间】:2016-08-23 04:45:21
【问题描述】:
我正在构建一个 Android 应用程序,从 Ruby On Rails 框架中构建的 API 获取数据。
这是我的时间戳字符串
2000-01-01T12:00:00.000Z
我需要将其转换为可读的日期和时间格式,即“2000 年 1 月 1 日星期六 12:00 am”
【问题讨论】:
我正在构建一个 Android 应用程序,从 Ruby On Rails 框架中构建的 API 获取数据。
这是我的时间戳字符串
2000-01-01T12:00:00.000Z
我需要将其转换为可读的日期和时间格式,即“2000 年 1 月 1 日星期六 12:00 am”
【问题讨论】:
你可以试试这个:
public static String convertDate(String oldDateString){
String format = "yyyy-MM-dd'T'HH:mm:ss.sssZZZZ";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(oldDateString);
} catch (ParseException e) {
// handle exception here !
}
String newFormatString = "MMMM dd, yyyy 'at' HH:mm a";
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
String newDateString = newFormatter.format(date);
return newDateString;
}
【讨论】:
我遇到了同样的问题,我尝试了@real19 的答案,但它给了我错误。 我当时就来了:
public static String convertDate(String oldDateString){
String format = "yyyy-MM-dd'T'HH:mm:ss.sss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(oldDateString);
} catch (ParseException e) {
// handle exception here !
e.printStackTrace();
}
String newFormatString = "EEEE MMMM dd, yyyy 'at' HH:mm a";
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
String newDateString = newFormatter.format(date);
return newDateString;
}
格式化给了我jeudi décembre 22, 2016 at 16:42 PM,但你可以调整你的 DateFormat
【讨论】: