LocalDate 常用api(LocalTime,LocalDateTime用法类似) jdk8及以上

		//获取当前日期
		LocalDate now = LocalDate.now();
		//获取今年的年份
		now.getYear();
		//获取本月的月份
		now.getMonth();
		//获取本月的第几天
		now.getDayOfMonth();
        //获取当前时间
        LocalTime.now();
        //获取当前日期+时间
        LocalDateTime.now();
        //构造日期
        LocalDate.of(2020,06,05);
        //构造日期
        LocalDate.parse("2020-06-05");
        //本月第一天 
        LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        //本月 第n天
        LocalDate.now().withDayOfMonth(n);
        //本月最后一天
        LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        
        //计算已过去的日期
		LocalDate.now().minusDays(1);//昨天
		LocalDate.now().minusWeeks(1);//上周
		LocalDate.now().minusMonths(1);//上个月
		LocalDate.now().minusYears(1);//上一年
		
		//计算将来的日期(加法)
		LocalDate.now().plusDays(1);//明天
		LocalDate.now().plusWeeks(1);//下周
		LocalDate.now().plusMonths(1);//下个月的今天
		LocalDate.now().plusYears(1);//明年
		
		//计算相差天数,切记不要使用Period.between()[因为跨月的话天数会重置,还要自己去计算月份,甚至年份]   要用 ChronoUnit.DAYS.between
		LocalDate date = LocalDate.of(2020,06,16);
        //当天日期
        LocalDate nowDate = LocalDate.now();
        //计算2020-06-05 距离今天相差多少天 (返回结果大于0,表示date是较小的时间,反之,date是较大的时间,等于0,代表是同一天) 
        ChronoUnit.DAYS.between(date, nowDate);
        
		//LocalDateTime或ZonedLocalDateTime时,我们要进行格式化显示,就要使用DateTimeFormatter
		//获取当前日期
		LocalDate now = LocalDate.now();
		String format = DateTimeFormatter.ofPattern("yyyy:MM:dd").format(now);
		System.out.println(format);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是关于JDK 8日期相关类的介绍和示例: 1. LocalDate类: - LocalDate类表示一个不可变的日期对象,它只包含日期部分(年、月、日)。 - 使用`now()`方法获取当前日期。 - 使用`of()`方法创建指定日期。 - 使用`getXXX()`方法获取日期的年、月、日等部分。 - 使用`plusXXX()`和`minusXXX()`方法进行日期的加减操作。 - 使用`isXXX()`方法判断日期的属性,如是否为闰年等。 示例代码: ```java import java.time.LocalDate; // 获取当前日期 LocalDate currentDate = LocalDate.now(); System.out.println("当前日期: " + currentDate); // 创建指定日期 LocalDate specificDate = LocalDate.of(2022, 1, 1); System.out.println("指定日期: " + specificDate); // 获取日期的年、月、日 int year = currentDate.getYear(); int month = currentDate.getMonthValue(); int day = currentDate.getDayOfMonth(); System.out.println("年: " + year + ", 月: " + month + ", 日: " + day); // 日期的加减操作 LocalDate futureDate = currentDate.plusDays(7); LocalDate pastDate = currentDate.minusMonths(1); System.out.println("未来日期: " + futureDate); System.out.println("过去日期: " + pastDate); // 判断是否为闰年 boolean isLeapYear = currentDate.isLeapYear(); System.out.println("是否为闰年: " + isLeapYear); ``` 2. LocalTime类: - LocalTime类表示一个不可变的时间对象,它只包含时间部分(时、分、秒、纳秒)。 - 使用`now()`方法获取当前时间。 - 使用`of()`方法创建指定时间。 - 使用`getXXX()`方法获取时间的时、分、秒等部分。 - 使用`plusXXX()`和`minusXXX()`方法进行时间的加减操作。 示例代码: ```java import java.time.LocalTime; // 获取当前时间 LocalTime currentTime = LocalTime.now(); System.out.println("当前时间: " + currentTime); // 创建指定时间 LocalTime specificTime = LocalTime.of(12, 30, 0); System.out.println("指定时间: " + specificTime); // 获取时间的时、分、秒 int hour = currentTime.getHour(); int minute = currentTime.getMinute(); int second = currentTime.getSecond(); System.out.println("时: " + hour + ", 分: " + minute + ", 秒: " + second); // 时间的加减操作 LocalTime futureTime = currentTime.plusHours(2); LocalTime pastTime = currentTime.minusMinutes(30); System.out.println("未来时间: " + futureTime); System.out.println("过去时间: " + pastTime); ``` 3. LocalDateTime类: - LocalDateTime类表示一个不可变的日期时间对象,它包含日期和时间部分。 - 使用`now()`方法获取当前日期时间。 - 使用`of()`方法创建指定日期时间。 - 使用`getXXX()`方法获取日期时间的年、月、日、时、分、秒等部分。 - 使用`plusXXX()`和`minusXXX()`方法进行日期时间的加减操作。 示例代码: ```java import java.time.LocalDateTime; // 获取当前日期时间 LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("当前日期时间: " + currentDateTime); // 创建指定日期时间 LocalDateTime specificDateTime = LocalDateTime.of(2022, 1, 1, 12, 30, 0); System.out.println("指定日期时间: " + specificDateTime); // 获取日期时间的年、月、日、时、分、秒 int year = currentDateTime.getYear(); int month = currentDateTime.getMonthValue(); int day = currentDateTime.getDayOfMonth(); int hour = currentDateTime.getHour(); int minute = currentDateTime.getMinute(); int second = currentDateTime.getSecond(); System.out.println("年: " + year + ", 月: " + month + ", 日: " + day); System.out.println("时: " + hour + ", 分: " + minute + ", 秒: " + second); // 日期时间的加减操作 LocalDateTime futureDateTime = currentDateTime.plusDays(7); LocalDateTime pastDateTime = currentDateTime.minusMonths(1); System.out.println("未来日期时间: " + futureDateTime); System.out.println("过去日期时间: " + pastDateTime); ``` 4. Calendar类: - Calendar类是Java旧版的日期时间处理类,JDK 8之后推荐使用新的日期时间API。 - Calendar类可以用于获取和设置日期时间的各个部分,如年、月、日、时、分、秒等。 - 使用`getInstance()`方法获取当前日期时间的Calendar实例。 - 使用`get()`方法获取日期时间的各个部分。 - 使用`set()`方法设置日期时间的各个部分。 示例代码: ```java import java.util.Calendar; // 获取当前日期时间的Calendar实例 Calendar currentCalendar = Calendar.getInstance(); System.out.println("当前日期时间: " + currentCalendar.getTime()); // 获取日期时间的年、月、日、时、分、秒 int year = currentCalendar.get(Calendar.YEAR); int month = currentCalendar.get(Calendar.MONTH) + 1; // 月份从0开始,需要加1 int day = currentCalendar.get(Calendar.DAY_OF_MONTH); int hour = currentCalendar.get(Calendar.HOUR_OF_DAY); int minute = currentCalendar.get(Calendar.MINUTE); int second = currentCalendar.get(Calendar.SECOND); System.out.println("年: " + year + ", 月: " + month + ", 日: " + day); System.out.println("时: " + hour + ", 分: " + minute + ", 秒: " + second); // 设置日期时间的年、月、日、时、分、秒 currentCalendar.set(Calendar.YEAR, 2022); currentCalendar.set(Calendar.MONTH, 0); // 月份从0开始,0表示1月 currentCalendar.set(Calendar.DAY_OF_MONTH, 1); currentCalendar.set(Calendar.HOUR_OF_DAY, 12); currentCalendar.set(Calendar.MINUTE, 30); currentCalendar.set(Calendar.SECOND, 0); System.out.println("设置后的日期时间: " + currentCalendar.getTime()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值