我14-03-2014 09:02:10
从 JSON 中得到这个字符串。一切都好,但我只想要这个字符串中的 date 部分而不是 time (14-03-2014)
。那么如何将字符串转换为这种格式。
问问题
753 次
6 回答
1
使用substring
方法
String jsonString = "14-03-2014 09:02:10";
if(jsonString.contains(" ")){
String onlyDate = string.substring(0, string.indexOf(" "));
}
于 2014-04-03T04:57:41.060 回答
0
您只需尝试此解决方案,我希望它对您有用,只需将日期格式转换为您可以获得所需的格式...
String inputPattern = "yyyy-MM-dd hh:mm:ss";
String outputPattern = "dd/MM";
SimpleDateFormat inFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date1 = null;
if (date != null) {
try {
date1 = inFormat.parse(date);
str = outputFormat.format(date1);
Log.d(null, str);
} catch (ParseException e) {
e.printStackTrace();
}
} else {
str = " ";
}
于 2014-04-03T04:55:49.703 回答
0
如果你已经有了String
, asubstring
会做的工作:
date = date.substring(0,10);
于 2014-04-03T04:56:56.897 回答
0
您可以使用 Split() 方法从字符串中提取日期部分
String str = "14-03-2014 09:02:10"
String [] part = str.split(" ");
String date = part[0];
或者,如果您的日期字段的大小是恒定的,您也可以使用substring()
方法,即日期部分中的 01 而不是 1
str = str.substring(0,10);
于 2014-04-03T04:57:39.893 回答
0
尝试这个...
public class Test {
public static void main(String a[])
{
String date_time = "14-03-2014 09:02:10";
String[] split_val = date_time.split(" ");
String date = split_val[0];
String time = split_val[1];
System.out.println(date+"--------------"+time);
}
}
于 2014-04-03T04:58:29.453 回答
0
使用下面的代码
String date="14-03-2014 09:02:10";
String reqdate=date.substring(0,10);
于 2014-04-03T05:03:49.367 回答