/**
* 将String类型转化为Date时间格式
* @param str
* @param id
* @return
*/
public static Date ToDateTime(String str, int id) {
SimpleDateFormat format = null;
switch (id) {
case 1:
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
case 2:
format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
break;
case 3:
format = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
break;
case 4:
format = new SimpleDateFormat("yyyy-MM-dd");
break;
}
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
* 将String类型转化为Date时间格式
* @param str
* @param id
* @return
*/
public static Date ToDateTime(String str, int id) {
SimpleDateFormat format = null;
switch (id) {
case 1:
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
case 2:
format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
break;
case 3:
format = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
break;
case 4:
format = new SimpleDateFormat("yyyy-MM-dd");
break;
}
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
本文介绍了一种在Java中将字符串格式的日期转换为Date对象的方法。该方法提供了多种日期格式的支持,包括“yyyy-MM-dd HH:mm:ss”、“yyyy/MM/dd HH:mm:ss”等,并通过一个简单的函数实现不同格式之间的灵活转换。
868

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



