JDK8中 LocalDate、LocalTime 、LocalDateTime 的用法介绍

在JAVA中,常用的处理日期和时间的类主要有Date,Calendar,而在JDK1.8中,新增了两个处理日期和时间的类,一个是LocalDate,另一个是LocalTime,下面我来介绍一下这两个类中常用方法的用法。

1、LocalDate

  LocalDate主要是用来处理日期的类,主要有以下方法:

方法说明举例
LocalDate.now()获取当前日期
LocalDate localDate = LocalDate.now();
System.out.println(localDate);

 运行结果为:2019-01-07

LocalDate.of(int year, int month, int dayOfMonth)

根据参数设置日期,参数分别为年,月,日

LocalDate localDate = LocalDate.of(2019,1,7);
System.out.println(localDate);

 运行结果为:2019-01-07

localDate.getDayOfMonth()

localDate.getDayOfWeek()

localDate.getDayOfYear()

获取当前日期是所在月的第几天

获取当前日期是星期几(星期的英文全称)

获取当前日期是所在年的第几天

LocalDate localDate = LocalDate.of(2019,1,7);
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.getDayOfWeek());
System.out.println(localDate.getDayOfYear());

运行结果为:7,MONDAY,7

localDate.getMonth()

localDate.getMonthValue()

localDate.lengthOfMonth()

获取当前日期所在月份(月份的英文全称)

获取当前日期所在月份的数值

获取当前日期所在月份有多少天

LocalDate localDate = LocalDate.of(2019,1,7);
System.out.println(localDate.getMonth());
System.out.println(localDate.getMonthValue());
System.out.println(localDate.lengthOfMonth());

运行结果为:JANUARY,1,31

 

localDate.lengthOfYear()

localDate.isLeapYear()

获取当前日期所在年有多少天

获取当前日期所在年是否是闰年

LocalDate localDate = LocalDate.of(2019,1,7);
System.out.println(localDate.lengthOfYear());
System.out.println(localDate.isLeapYear());

 运行结果为:365,false

localDate.withDayOfMonth(int dayOfMonth)

localDate.withDayOfYear(int dayOfYear)

localDate.withMonth(int month)

localDate.withYear(int year)

with开头的方法,我的理解是将参数替换localDate中的对应

属性,重新计算得到新的日期。

将参数中的"日"替换localDate中的"日"

将参数中的天数替换localDate中的天数

将参数中的"月"替换localDate中的"月"

将参数中的"年"替换localDate中的"年"

LocalDate localDate = LocalDate.of(2019,1,7);
System.out.println(localDate.withDayOfMonth(2));
System.out.println(localDate.withDayOfYear(40));
System.out.println(localDate.withMonth(2));
System.out.println(localDate.withYear(2020));

 运行结果为:

2019-01-02,

2019-02-09,

2019-02-07,

2020-01-07

localDate.minusDays(long days)

localDate.minusWeeks(long weeks)

localDate.minusMonths(long months)

localDate.minusYears(long years)

localDate.plusDays(long days)

localDate.plusWeeks(long weeks)

localDate.plusMonths(long months)

localDate.plusYears(long years) 

将当前日期减一天

将当前日期减一周

将当前日期减一月

将当前日期减一年

将当前日期加一天

将当前日期加一周

将当前日期加一月

将当前日期加一年

2、LocalTime

  LocalTime主要是用来处理时间的类,主要有以下方法:

方法说明举例
LocalTime.now()获取当前时间
LocalTime localTime = LocalTime.now();
System.out.println(localTime);

 运行结果为:22:53:44.277

LocalTime.of(int hour, int minute) 

LocalTime.of(int hour, int minute, int second)

根据参数设置时间,参数分别为时,分

根据参数设置时间,参数分别为时,分,秒

LocalTime localTime = LocalTime.of(12,35,59);
System.out.println(localTime);
localTime = LocalTime.of(12,35);
System.out.println(localTime);

 运行结果为:12:35:59,12:35

localTime.getHour()

localTime.getMinute()

localTime.getSecond()

获取当前时间的小时数

获取当前时间的分钟数

获取当前时间的秒数

LocalTime localTime = LocalTime.of(12,35,59);
System.out.println(localTime.getHour());
System.out.println(localTime.getMinute());
System.out.println(localTime.getSecond());

运行结果为:12,35,59

localTime.withHour(int hour)

localTime.withMinute(int minute)

localTime.withSecond(int second)

with开头的方法,我的理解是将参数替换localTime中的对应属性,

重新计算得到新的时间。

将参数中的"小时"替换localTime中的"小时"

将参数中的"分钟"替换localTime中的"分钟"

将参数中的"秒"替换localTime中的"秒"

LocalTime localTime = LocalTime.of(12,35,59);
System.out.println(localTime.withHour(1));
System.out.println(localTime.withMinute(1));
System.out.println(localTime.withSecond(1));

 运行结果为:

01:35:59
12:01:59
12:35:01

localTime.minusHours(long hours)

localTime.minusMinutes(long minutes)

localTime.minusSeconds(long seconds)

localTime.plusHours(long hours)

localTime.plusMinutes(long minutes)

localTime.plusSeconds(long seconds)

将当前时间减一小时

将当前时间减一分钟

将当前时间减一秒

将当前时间加一小时

将当前时间加一分钟

将当前时间加一秒

3、  TemporalAdjuster (时间矫正器   )

        //获取下个周末
		LocalDate date = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
		System.out.println(date);

		//获取下一个工作日
		LocalDateTime ldt1 = LocalDateTime.now();
		LocalDateTime ldt5 = ldt1.with((l)->{
			LocalDateTime ldt4 = (LocalDateTime) l;
			DayOfWeek dayOfWeek = ldt4.getDayOfWeek();
			if(dayOfWeek.equals(DayOfWeek.FRIDAY)){
				return ldt4.plusDays(3);
			}else if(dayOfWeek.equals(DayOfWeek.SATURDAY)){
				return ldt4.plusDays(2);
			}else {
				return ldt4.plusDays(1);
			}
		});
		System.out.println(ldt5);

4、  日期转换

       DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
		LocalDateTime ldf = LocalDateTime.now();
		String str1 = ldf.format(dtf1);
		System.out.println(str1);
		
		DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
		LocalDate date2 = LocalDate.now().parse("2019-08-28",dtf);
		System.out.println(date2);

5、  日期计算

//		Duration  计算两个时间的间隔
		LocalTime now1 = LocalTime.now();
		LocalTime now2 = LocalTime.now();
		Duration between = Duration.between(now1,now2);
		System.out.println(between.getSeconds());
		//		Period  计算两个日期的间隔
		LocalDate dateTime1 = LocalDate.of(2018,8,28);
		LocalDate dateTime2 = LocalDate.now();
		Period period = Period.between(dateTime1,dateTime2);
		System.out.println(period.getYears()+"-"+period.getMonths()+"-"+period.getDays());

 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是关于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()); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值