在android中,如何从服务器响应的字符串中分割日期?
我有一个类似2017-05-22 12:57:46.688的字符串,我必须从中分离日期,并希望更改其格式,如dd/MM/yyyy,并将其设置为文本视图 代码如下:在android中,如何从服务器响应的字符串中分割日期?,android,Android,我有一个类似2017-05-22 12:57:46.688的字符串,我必须从中分离日期,并希望更改其格式,如dd/MM/yyyy,并将其设置为文本视图 代码如下: String transactionDate = post.getString(CEarningHistory.TRANSACTION_CTS); String[] parts = transactionDate.split(" "); SimpleDateFormat input = new SimpleDateFormat(par
String transactionDate = post.getString(CEarningHistory.TRANSACTION_CTS);
String[] parts = transactionDate.split(" ");
SimpleDateFormat input = new SimpleDateFormat(parts[0]);
尝试这种方式,您需要传递服务器日期时间格式和输出格式
String yourDate = formatDateForString(inputFormat, outputFormat, inputDateTime);
public String formatDateForString(String inputFormat, String outputFormat, String inputDate) {
Date parsed;
String outputDate = "";
SimpleDateFormat df_input = new SimpleDateFormat(inputFormat);
SimpleDateFormat df_output = new SimpleDateFormat(outputFormat);
try {
parsed = df_input.parse(inputDate);
outputDate = df_output.format(parsed);
} catch (ParseException e) {
LOGE("DateTime", "ParseException - dateFormat");
}
return outputDate.toUpperCase().replace(".","");
}
试试这个
SimpleDateFormat webFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
webFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String newDate = dateFormat.format(webFormat.parse("YourdateinWebFormat"));
应使用SimpleDataFormat解析字符串,ideallyAlso或检查此字符串