1、Date转String
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String str = dateFormat.format(date);
System.out.println(date); // Fri Jul 24 17:25:38 CST 2020
System.out.println(str); // 2020-07-24 17:25:38
2、String转Date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = "2020-07-24 14:40:30";
Date date = dateFormat.parse(str);
System.out.println(dateFormat.format(date)); // 2020-07-24 14:40:30
3、Date转Calendar
Calendar calendar = Calendar.getInstance();
Date date = new Date();
calendar.setTime(date);
System.out.pringln(calendar); // java.util.GregorianCalendar[time=1595583092933,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2020,MONTH=6,WEEK_OF_YEAR=30,WEEK_OF_MONTH=4,DAY_OF_MONTH=24,DAY_OF_YEAR=206,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=31,SECOND=32,MILLISECOND=933,ZONE_OFFSET=28800000,DST_OFFSET=0]
4、Calendar转Date
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
System.out.println(date); // Fri Jul 24 17:33:59 CST 2020
5、Date转时间戳
Date date = new Date();
long time = date.getTime();
System.out.println(time); // 1595583344524
6、时间戳转Date
Date date = new Date(1595583344524L);
System.out.println(date); // Fri Jul 24 17:35:44 CST 2020