Java 8 日期新特性
日期格式的互相转换
转载:https://www.cnblogs.com/puke/p/11314431.html
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");LocalDateTime localDateTime = LocalDateTime.parse("2019-07-31 00:00:00",dateTimeFormatter1);LocalDate localDate = LocalDate.parse("2019-07-31",dateTimeFormatter2);Date date = Date.from(LocalDateTime.parse("2019-07-31 00:00:00",dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant());String strDateTime = "2019-07-31 00:00:00";String strDate = "2019-07-31";Long timestamp=1564502400000l;/** LocalDateTime 转 LocalDate */System.out.println("LocalDateTime 转 LocalDate: "+localDateTime.toLocalDate());/** LocalDateTime 转 Long */System.out.println("LocalDateTime 转 Long: "+localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());/** LocalDateTime 转 Date */System.out.println("LocalDateTime 转 Date: "+Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));/** LocalDateTime 转 String */System.out.println("LocalDateTime 转 String: "+localDateTime.format(dateTimeFormatter1));System.out.println("-------------------------------");/** LocalDate 转 LocalDateTime */System.out.println("LocalDate 转 LocalDateTime: "+LocalDateTime.of(localDate,LocalTime.parse("00:00:00")));/** LocalDate 转 Long */System.out.println("LocalDate 转 Long: "+localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli());/** LocalDate 转 Date */System.out.println("LocalDate 转 Date: "+Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));/** LocalDate 转 String */System.out.println("LocalDateTime 转 String: "+localDateTime.format(dateTimeFormatter2));System.out.println("-------------------------------");/** Date 转 LocalDateTime */System.out.println("Date 转 LocalDateTime: "+LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));/** Date 转 Long */System.out.println("Date 转 Long: "+date.getTime());/** Date 转 LocalDate */System.out.println("Date 转 LocalDate: "+LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate());/** Date 转 String */SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS" );System.out.println("Date 转 String: "+sdf.format(date));System.out.println("-------------------------------");/** String 转 LocalDateTime */System.out.println("String 转 LocalDateTime: "+LocalDateTime.parse(strDateTime,dateTimeFormatter1));/** String 转 LocalDate */System.out.println("String 转 LocalDate: "+LocalDateTime.parse(strDateTime,dateTimeFormatter1).toLocalDate());System.out.println("String 转 LocalDate: "+LocalDate.parse(strDate,dateTimeFormatter2));/** String 转 Date */System.out.println("String 转 Date: "+Date.from(LocalDateTime.parse(strDateTime,dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant()));System.out.println("-------------------------------");/** Long 转 LocalDateTime */System.out.println("Long 转 LocalDateTime:"+LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()));/** Long 转 LocalDate */System.out.println("Long 转 LocalDate:"+LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).toLocalDate());LocalDateTime now = LocalDateTime.now();//2020-09-11T09:41:01.547LocalDateTime DiscountStartTime = now.minusDays(3);//减法 2020-09-08T09:41:01.547LocalDateTime DiscountEndTime = now.plusDays(3);//加法 2020-09-14T09:41:01.547// now>DiscountStartTime && DiscountEndTime>now// DiscountEndTime>now>DiscountStartTimeif(now.isAfter(DiscountStartTime) && DiscountEndTime.isAfter(now)){true;//输出true}
日期处理
@Testpublic void contextLoads() {LocalDate today = LocalDate.now(); // 当前日期 年 月 日LocalTime minTime = LocalTime.MIN; // 最小时间 00:00:00LocalDate twoDaysLaterDate = today.plusDays(2); // 两天之后的日期 年 月 日LocalTime maxTime = LocalTime.MAX; // 最大时间 23:59:59// 将日期和时间组合起来,并转换成想要的格式String beginDate = LocalDateTime.of(today, minTime).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));String endDate = LocalDateTime.of(twoDaysLaterDate, maxTime).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println("开始时间:" + beginDate);System.out.println("结束时间:" + endDate);}
获得一个月最大天数的方法(备忘)
获取某段时间内的周几的日期
/*** 获取某段时间内的周几的日期** @param dataBegin 开始日期* @param dataEnd 结束日期* @param weekDays 获取周几,1-6代表周一到周六。0代表周日* @return 返回日期List*/public static List<String> getDayOfWeekWithinDateInterval(String dataBegin, String dataEnd, int weekDays) {SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");List<String> dateResult = new ArrayList<>();Calendar cal = Calendar.getInstance();String[] dateInterval = {dataBegin, dataEnd};Date[] dates = new Date[dateInterval.length];for (int i = 0; i < dateInterval.length; i++) {String[] ymd = dateInterval[i].split("[^\\d]+");cal.set(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1, Integer.parseInt(ymd[2]));dates[i] = cal.getTime();}for (Date date = dates[0]; date.compareTo(dates[1]) <= 0; ) {cal.setTime(date);if (cal.get(Calendar.DAY_OF_WEEK) - 1 == weekDays) {String format = sd.format(date);dateResult.add(format);}cal.add(Calendar.DATE, 1);date = cal.getTime();}return dateResult;}
获取两个指定日期之间的所有月份
public void getAmongstMonth() {String y1 = "2022-03";// 开始时间String y2 = "2022-05";// 结束时间try {Date startDate = new SimpleDateFormat("yyyy-MM").parse(y1);Date endDate = new SimpleDateFormat("yyyy-MM").parse(y2);Calendar calendar = Calendar.getInstance();calendar.setTime(startDate);// 获取开始年份和开始月份int startYear = calendar.get(Calendar.YEAR);int startMonth = calendar.get(Calendar.MONTH);// 获取结束年份和结束月份calendar.setTime(endDate);int endYear = calendar.get(Calendar.YEAR);int endMonth = calendar.get(Calendar.MONTH);//List<String> list = new ArrayList<String>();for (int i = startYear; i <= endYear; i++) {String date = "";if (startYear == endYear) {for (int j = startMonth; j <= endMonth; j++) {if (j < 9) {date = i + "-0" + (j + 1);} else {date = i + "-" + (j + 1);}list.add(date);}} else {if (i == startYear) {for (int j = startMonth; j < 12; j++) {if (j < 9) {date = i + "-0" + (j + 1);} else {date = i + "-" + (j + 1);}list.add(date);}} else if (i == endYear) {for (int j = 0; j <= endMonth; j++) {if (j < 9) {date = i + "-0" + (j + 1);} else {date = i + "-" + (j + 1);}list.add(date);}} else {for (int j = 0; j < 12; j++) {if (j < 9) {date = i + "-0" + (j + 1);} else {date = i + "-" + (j + 1);}list.add(date);}}}}// 所有的月份已经准备好//System.out.println(list);for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}System.out.println("里面有" + list.size() + "个月份");} catch (Exception e) {e.printStackTrace();}}
对当前时间进行加减
// 将分钟转换为毫秒int fiveMinutes = 5 * 60 * 1000;Date now = new Date();// 5分钟之后Date fiveMinutesAgo = new Date(now.getTime() + fiveMinutes);// 5分钟之前Date fiveMinutesAgo = new Date(now.getTime() - fiveMinutes);
两个日期中间有多少天
/*** 两个日期中间有多少天** @param startDate* @param endDate* @return*/public static Long getAmongstDays(Date startDate, Date endDate) {long intervalMills = endDate.getTime() - startDate.getTime();return intervalMills / (1000 * 60 * 60 * 24);}
