Java日期格式转换
1.Date转String
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
2.String转Date
String dateString = "2021/2/20 15:45:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(dateString);
Java日期格式增加时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
// 1.添加毫秒(5分钟)
Date date1 = new Date(now.getTime() + 1000*60*5);
// 2.Calendar添加 (YEAR 年、MONTH 月、DATE 日、HOUR_OF_DAY 时、MINUTE 分、SECOND/MILLISECOND 秒/毫秒)
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 5);
Date date2 = calendar.getTime();