第四十一节 使用java如何计算年月日

亮子 2025-05-17 08:18:38 341 0 0 0

1、如何获取当前月、上个月、下个月

import java.time.LocalDate;
import java.time.Month;

public class MonthNavigation {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        
        // 上个月
        LocalDate lastMonth = today.minusMonths(1);
        int lastMonthValue = lastMonth.getMonthValue();
        Month lastMonthEnum = lastMonth.getMonth();
        
        // 下个月
        LocalDate nextMonth = today.plusMonths(1);
        int nextMonthValue = nextMonth.getMonthValue();
        Month nextMonthEnum = nextMonth.getMonth();
        
        System.out.println("当前月份: " + today.getMonthValue());
        System.out.println("上个月数字: " + lastMonthValue);
        System.out.println("上个月名称: " + lastMonthEnum);
        System.out.println("下个月数字: " + nextMonthValue);
        System.out.println("下个月名称: " + nextMonthEnum);
    }
}

2、如何获得昨天、今天、明天的日期

import java.time.LocalDate;

public class DateNavigation {
    public static void main(String[] args) {
        // 今天
        LocalDate today = LocalDate.now();
        
        // 昨天
        LocalDate yesterday = today.minusDays(1);
        
        // 明天
        LocalDate tomorrow = today.plusDays(1);
        
        System.out.println("今天: " + today);
        System.out.println("昨天: " + yesterday);
        System.out.println("明天: " + tomorrow);
    }
}

3、如何获得今年、明年、去年?

import java.time.LocalDate;

public class YearNavigation {
    public static void main(String[] args) {
        // 今年
        int currentYear = LocalDate.now().getYear();
        
        // 去年
        int lastYear = currentYear - 1;
        
        // 明年
        int nextYear = currentYear + 1;
        
        System.out.println("今年: " + currentYear);
        System.out.println("去年: " + lastYear);
        System.out.println("明年: " + nextYear);
    }
}

4、如何格式化LocalDate类型的变量为年月日时分秒字符串

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeFormatting {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        
        // 创建格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        
        // 格式化
        String formattedDateTime = now.format(formatter);
        
        System.out.println("格式化结果: " + formattedDateTime);
    }
}

5、如何打印出上个月的每一天?

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

public class LastMonthDaysPrinter {
    public static void main(String[] args) {
        // 获取上个月的YearMonth对象
        YearMonth lastMonth = YearMonth.now().minusMonths(1);
        
        // 获取上个月的天数
        int daysInMonth = lastMonth.lengthOfMonth();
        
        // 创建日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        
        // 打印上个月的每一天
        for (int day = 1; day <= daysInMonth; day++) {
            LocalDate date = lastMonth.atDay(day);
            System.out.println(date.format(formatter));
        }
    }
}