JAVA8 日期运算

226 阅读3分钟

计算 日期间隔月份,最大时间、最小时间


    public static void main(String[] args) {
        // 定义开始时间和结束时间
        String startTime = "2023-01";
        String endTime = "2023-03";

        // 解析日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        YearMonth startLocalDate = YearMonth.parse(startTime, formatter);
        YearMonth endLocalDate = YearMonth.parse(endTime, formatter);

        // 递减循环
        // while (endLocalDate.isAfter(startLocalDate) || endLocalDate.equals(startLocalDate)) {
        while (!endLocalDate.isBefore(startLocalDate)) {
            // 输出当前日期
            System.out.println(endLocalDate.format(formatter));

            LocalDate localDate = endLocalDate.atDay(1);
            ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
            Date date = Date.from(zonedDateTime.toInstant());
            System.out.println(date);
            LocalDateTime startOfMonth = endLocalDate.atDay(1).atStartOfDay();
            Date dateStart00 = Date.from(startOfMonth.atZone(ZoneId.systemDefault()).toInstant());

            System.out.println(dateStart00);

            LocalDate ld= endLocalDate.atEndOfMonth();
            LocalDateTime endOfMonth = ld.atTime(23, 59, 59);
             Date dateEnd59 = Date.from(endOfMonth.atZone(ZoneId.systemDefault()).toInstant());
            System.out.println(dateEnd59);

            // 减少一个月
            endLocalDate = endLocalDate.minusMonths(1);
            
        }

    }

}
2023-03
Wed Mar 01 00:00:00 CST 2023
Wed Mar 01 00:00:00 CST 2023
Fri Mar 31 23:59:59 CST 2023
2023-02
Wed Feb 01 00:00:00 CST 2023
Wed Feb 01 00:00:00 CST 2023
Tue Feb 28 23:59:59 CST 2023
2023-01
Sun Jan 01 00:00:00 CST 2023
Sun Jan 01 00:00:00 CST 2023
Tue Jan 31 23:59:59 CST 2023

时间比较

public static void main(String[] args) throws Exception {
    Date s =  DateUtil.stringToDate("2023-01-27 ","yyyy-MM-dd");
    System.out.println( s.before(new Date()));//true
    Date e =  DateUtil.stringToDate("2023-09-01 ","yyyy-MM-dd");
    System.out.println( e.after(new Date()));//true
    boolean isMatchTime =(s.before(new Date()) && e.after(new Date()));
    System.out.println(isMatchTime);//true
    boolean isNotMatchTime =(s.after(new Date()) || e.before(new Date()));
    System.out.println(isNotMatchTime);//false
}

获取当前时间 年、月 、日 、周、星期


public static void main(String[] a) {

    final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    //final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd w hh:mm:ss");

    final LocalDateTime currentDate = LocalDateTime.parse("2023-01-01 23:59:59", dateTimeFormatter);


    //获取当前时间
   // LocalDateTime currentDate = LocalDateTime.now();
    //获取年份
    int year = currentDate.getYear();
    System.out.println("获取当前年份:" + year);
    //获取月份
    int month = currentDate.getMonthValue();
    System.out.println("获取当前月份:" + month);

    //获取日
    int day = currentDate.getDayOfMonth();
    System.out.println("获取当前日:" + day);

    //获取当前周的星期几
    int week = currentDate.getDayOfWeek().getValue();
    System.out.println("获取今天星期几:" + week);

    //获取当前时间第X周
    WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1);
    int weeks = currentDate.get(weekFields.weekOfYear());
    System.out.println("获取当前时间第" + weeks + "周");


}
 

获取当前年份:2023 获取当前月份:1 获取当前日:1 获取今天星期几:7 获取当前时间第1周

LocalDateTime转换

string类和LocalDateTime的相互转换方式

//1.具有转换功能的对象
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//2.要转换的对象    
LocalDateTime time = LocalDateTime.now();

//3.发动功能
String localTime = df.format(time);
System.out.println("LocalDateTime转成String类型的时间:"+localTime);

//3.LocalDate发动,将字符串转换成  df格式的LocalDateTime对象,的功能
LocalDateTime LocalTime = LocalDateTime.parse(strLocalTime,df)
System.out.println("String类型的时间转成LocalDateTime:"+LocalTime);

LocalDate转换

DateTimeFormatter struct = DateTimeFormatter.ofPattern("yyyy-MM-dd")
LocalDate localDate = LocalDate.now();
String format = struct.format(localDate)
System.out.println("LocalDate转成String类型的时间:"+format)
LocalDate parse = LocalDate.parse(format
System.out.println("String类型的时间转成LocalDateTime:"+parse);


string转化LocalDateTime类出现的问题

原因是因为时间格式中的小时 hh 采用12小时,反解析时不知道上午还是下午,改成 "yyyy-MM-dd w hh:mm:ss a"或者采用24小时制“yyyy-MM-dd w HH:mm:ss”


LocalDateTime now = LocalDateTime.now();
final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd w hh:mm:ss");
final String format = now.format(dateTimeFormatter);
System.out.println(format);
final LocalDateTime parse = LocalDateTime.parse(format, dateTimeFormatter);

异常如下:

Text '2023-01-01 23:59:59' could not be parsed at index 13

Exception in thread "main" java.time.format.DateTimeParseException: Text '2023-01-01' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2023-01-01 of type java.time.format.Parsed