/**
* 将Date类型装换为String类型的时间格式
* @param id // 转换格式的ID
* @param date // 时间参数
* @return
*/
public static String ToStringTime(int id, Date date) {
String resultTime = null;
Long time = System.currentTimeMillis();
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("HH:mm");
break;
case 5:
format = new SimpleDateFormat("MM月dd日HH:mm");
break;
case 6:
format = new SimpleDateFormat("yyyy-MM-dd");
break;
}
resultTime = format.format(date);
return resultTime;
}
* 将Date类型装换为String类型的时间格式
* @param id // 转换格式的ID
* @param date // 时间参数
* @return
*/
public static String ToStringTime(int id, Date date) {
String resultTime = null;
Long time = System.currentTimeMillis();
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("HH:mm");
break;
case 5:
format = new SimpleDateFormat("MM月dd日HH:mm");
break;
case 6:
format = new SimpleDateFormat("yyyy-MM-dd");
break;
}
resultTime = format.format(date);
return resultTime;
}
本文介绍了一个实用的Java方法,该方法可以根据不同的格式ID将Date类型的日期转换为String格式的时间字符串。通过简单的switch-case结构实现了多种日期格式的支持,如yyyy-MM-dd HH:mm:ss、MM月dd日HH:mm等。
3701

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



