转换时间的方法
/**
* 时间转换
* @param str
* @return
*/
public static String reversalTime(String str) {
String result="";
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'CST' yyyy",Locale.US);
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
result=sdf.format(df.parse(str));
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}测试方法:public static void main(String[] args) {
Date date = new Date();
String time=date.toString();
System.out.println(time);
String res=TimeUtil.reversalTime(time);
System.out.println(res);
}
本文介绍了一种将特定格式的时间字符串转换为另一种常见格式的方法。通过使用SimpleDateFormat类定义输入和输出的时间格式,该方法能够从一种复杂的国际标准格式转换为更易读的日期时间格式。此外,还提供了一个测试实例来验证转换功能的有效性。
1357

被折叠的 条评论
为什么被折叠?



