一、来由
每次遇到时间转换问题,总要去翻一翻浏览器,虽然很简单,但就是记不住,于是抽时间自己写了一个比较常用的时间转换工具类,以后直接copy过来用,以后有空再补充一些新的内容。
二、说明
所有方法实现中的参数,都做了非空校验
三、实现
package com.example.bean;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 日期转换工具类
* @author crz
* @date 2021/10/12
* @version 1.0
*/
public class DateCovertUtil {
private DateCovertUtil(){};
/**
* Date类型转String类型,标准格式:yyyy-MM-dd HH:mm:ss
* 例:Mon Oct 11 20:19:37 CST 2021 ---> 2021-10-11 20:19:37
* @param date
* @return String
*/
public static String dateToString(Date date){
if (date == null){
throw new RuntimeException("date参数为空");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
/**
* Date类型转String类型,自定义转换格式
* 常用可选格式:yyyy-MM-dd HH:mm:ss yyyy-MM-dd HH:mm yyyy-MM-dd HH yyyy-MM-dd yyyy-MM yyyy
* @param date
* @return String
*/
public static String dateToStringCustomizing(Date date,String formatString){
if (date == null || StringUtils.isEmpty(formatString)){
throw new RuntimeException("参数为空,参数date:"+date+" formatString:"+formatString);
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(formatString);
return sdf.format(date);
}catch (Exception e){
throw new RuntimeException("日期格式化设置错误:"+formatString);
}
}
/**
* String类型转Date类型,标准格式:yyyy-MM-dd HH:mm:ss
* 例:2021-10-11 20:19:37 ---> Mon Oct 11 20:19:37 CST 2021
* @param dateStr
* @return Date
*/
public static Date stringToDate(String dateStr) {
if (StringUtils.isEmpty(dateStr)){
throw new RuntimeException("日期字符串为空");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
try {
date = sdf.parse(dateStr);
}catch (ParseException e){
throw new RuntimeException("字符串转日期异常,字符串:"+dateStr);
}
return date;
}
/**
* String类型转Date类型,自定义转换格式
* 常用可选格式:yyyy-MM-dd HH:mm:ss yyyy-MM-dd HH:mm yyyy-MM-dd HH yyyy-MM-dd yyyy-MM yyyy
* @param dateStr
* @return Date
*/
public static Date stringToDateCustomizing(String dateStr,String formatString) {
if (StringUtils.isEmpty(dateStr) || StringUtils.isEmpty(formatString)){
throw new RuntimeException("参数为空,dateStr:"+dateStr+" formatString:"+formatString);
}
SimpleDateFormat sdf = new SimpleDateFormat(formatString);
Date date;
try {
date = sdf.parse(dateStr);
}catch (ParseException e){
throw new RuntimeException("自定义字符串转日期异常,字符串:"+dateStr+" 日期格式:"+formatString);
}
return date;
}
/**
* Date类型转Calendar类型
* @param date
* @return Calendar
*/
public static Calendar dateToCalendar(Date date) {
if (date == null){
throw new RuntimeException("date参数为空");
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
/**
* Calendar类型转Date类型
* @param calendar
* @return Date
*/
public static Date calendarToDate(Calendar calendar) {
if (calendar == null){
throw new RuntimeException("calendar参数为空");
}
return calendar.getTime();
}
/**
* Calendar类型转String类型
* @param calendar
* @return Date
*/
public static String calendarToString(Calendar calendar) {
return dateToString(calendarToDate(calendar));
}
/**
* String类型转Calendar类型
* @param dateString
* @return Calendar
*/
public static Calendar StringToCalendar(String dateString) {
return dateToCalendar(stringToDate(dateString));
}
/**
* Date类型获取当前年月日时分秒
* key值:year,month,day,hour(12小时制),minute,second,hourOfDay(24小时制),dayOfWeek
* @param date
* @return Map<String,Integer>
*/
public static Map<String,Integer> dateGetDetail(Date date){
Map<String,Integer> map = new HashMap<>();
if (date == null){
throw new RuntimeException("date参数为空");
}
Calendar calendar = dateToCalendar(date);
if (calendar.get(Calendar.YEAR) != 0) { map.put("year", calendar.get(Calendar.YEAR)); }
if (calendar.get(Calendar.MONTH) != 0) { map.put("month",calendar.get(Calendar.MONTH)+1); }
if (calendar.get(Calendar.DATE) != 0) { map.put("day",calendar.get(Calendar.DATE)); }
//12小时制
if (calendar.get(Calendar.HOUR) != 0) { map.put("hour",calendar.get(Calendar.HOUR)); }
if (calendar.get(Calendar.MINUTE) != 0) { map.put("minute",calendar.get(Calendar.MINUTE)); }
if (calendar.get(Calendar.SECOND) != 0) { map.put("second",calendar.get(Calendar.SECOND)); }
//24小时制
if (calendar.get(Calendar.HOUR_OF_DAY) != 0) { map.put("hourOfDay",calendar.get(Calendar.HOUR_OF_DAY)); }
//一周的第几天
if (calendar.get(Calendar.DAY_OF_WEEK) != 0) { map.put("dayOfWeek",calendar.get(Calendar.DAY_OF_WEEK)); }
return map;
}
/**
* 获取系统当前时间戳
* @return long
*/
public static long getCurrentTimeMillis(){
return System.currentTimeMillis();
}
}
四、测试
package com.example.bean;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
public class Test {
public static void main(String[] args) {
//参数准备
Date date = new Date();
Calendar calendar = Calendar.getInstance();
String dateStr = "2021-10-11 20:19:37";
//Date类型转String类型
String string1 = DateCovertUtil.dateToString(date);
System.out.println("Date类型转String类型:\n" + string1);
//Date类型转String类型,自定义转换格式
String string2 = DateCovertUtil.dateToStringCustomizing(date, "yyyy-MM-dd");
System.out.println("\nDate类型转String类型,自定义转换格式:\n" + string2);
//String类型转Date类型
Date date1 = DateCovertUtil.stringToDate(dateStr);
System.out.println("\nString类型转Date类型:\n"+date1);
//String类型转Date类型,自定义转换格式
Date date2 = DateCovertUtil.stringToDateCustomizing(dateStr, "yyyy-MM-dd");
System.out.println("\nString类型转Date类型,自定义转换格式:\n" + date2);
//Date类型转Calendar类型
Calendar calendar1 = DateCovertUtil.dateToCalendar(date);
System.out.println("\nDate类型转Calendar类型:\n" + calendar1);
//Calendar类型转Date类型
Date date3 = DateCovertUtil.calendarToDate(calendar);
System.out.println("\nCalendar类型转Date类型:\n" + date3);
//Date类型获取年月日时分秒
Map<String, Integer> dateMap = DateCovertUtil.dateGetDetail(date);
System.out.println("\nDate类型获取年月日时分秒:\n" + dateMap);
}
}
五、测试结果
六、注意
阿里规范中要求代码禁用 static 修饰 SimpleDateFormat,因为SimpleDateFormat是线程不安全的,所以涉及到了多线程可以使用DateTimeFormatter
具体请参考链接: https://blog.csdn.net/afreon/article/details/111878486