Calendar代替Date常用方法小记
引用:https://www.cnblogs.com/nyatom/p/9105375.html
// 获取当前各自日期实例
Calendar calendar = Calendar.getInstance();
Date date = new Date();
// 相互转换
Date temp = calendar.getTime();
calendar.setTime(date);
// 设置时分秒,Calendar完全替换Date
calendar.set(Calendar.HOUR_OF_DAY,9);
calendar.set(Calendar.MINUTE,30);
calendar.set(Calendar.SECOND,0);
// 时间比较,Calendar完全替换Date+1
Calendar calendar1 = Calendar.getInstance();
// before/after参数可为Calendar或Date
Boolean tem = calendar.before(calendar);
Boolean tem1 = calendar.after(calendar);
Boolean tem2 = calendar.after(date);
// 日期格式转换,commons-lang
// 日期转字符串 Date--String
String dateStr = DateFormatUtils.format(date,"yyyy-MM-dd HH:MM:SS");
// 字符串转日期 String--Date
String[] pattern = new String[]{"yyyy-MM","yyyyMM","yyyy/MM",
"yyyyMMdd","yyyy-MM-dd","yyyy/MM/dd",
"yyyyMMddHHmmss",
"yyyy-MM-dd HH:mm:ss",
"yyyy/MM/dd HH:mm:ss"};
String test = "2018-5-22";
Date date1 = DateUtils.parseDate(test, pattern);