JDK1.8之后时间日期相关类:LocalDate类、LocalTime类、LocalDateTime类

一、JDK1.7之前的时间表示缺点

  1. 非线程安全:java.util.Date是非线程安全的。
  2. 设计差:Date类在java.util包和java.sql包中都有,区分性差。
  3. 时间格式化和解析的类在java.text包中定义,而时间类在java.util包中定义。
  4. java.util.Date同时包含日期和时间,而java.sql.Date仅包含日期。
  5. 时区处理麻烦:日期类不提供国际化,没时区支持。

二、java8中time类

两个比较重要的API

  • Local(本地):简化了时间日期的处理,没有时区问题。
  • Zoned(时区):通过制定的时区处理日期时间。

三、LocalDateTime类、LocalDate类、LocalTime类

3.1 获取当前时间

public void test()
     {
    	 //获取当前日期时间
    	 LocalDateTime currenttime = LocalDateTime.now();
    	 System.out.println("当前时间:"+currenttime);
     }

输出:

当前时间:2020-12-30T11:48:12.732

3.2 获取当前日期

//获取当前的日期
    	 LocalDate date0  = LocalDate.now();
    	 System.out.println("date0"+date0);
    	 LocalDate date1 = currenttime.toLocalDate();
    	 System.out.println("date1"+date1);

输出:

date02020-12-30
date12020-12-30

3.3 获取年月日时分秒

//获取年月日时分秒
    	 Month month = currenttime.getMonth();
    	 int day = currenttime.getDayOfMonth();
    	 int seconds = currenttime.getSecond();
    	 System.out.println("月:"+month+",日:"+day+",秒:"+seconds);

结果:

月:DECEMBER,日:30,秒:6

3.4 设定年月日时分秒

//设定年月日时分秒
    	 LocalDateTime date2 = currenttime.withDayOfMonth(10).withYear(20);
    	 System.out.println("date2:"+date2);

结果:

date2:0020-12-10T13:44:58.166

3.5 设定日期

//设定日期12 december 2014
    	 LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 15);
    	 System.out.println("date3:"+date3);

结果:

date3:2014-12-15

3.6 设定时间

//设定时间 23 小时59分钟
    	 LocalTime time = LocalTime.of(23, 59);
    	 System.out.println("time:"+time);

结果:

time:23:59

3.6 字符串转化为时间

3.6.1 parse(CharSequence text)方法

用法: public static LocalTime parse(CharSequence text)
参数:此方法仅接受一个参数文本,该参数文本是在LocalTime中解析的文本。它不能为空。
返回值:此方法返回LocalTime,它是解析的本地日期时间。
异常:如果无法解析文本,则此方法将引发DateTimeParseException。

示例:

//类型转换:解析字符串
    	 LocalTime time1 = LocalTime.parse("20:15:36");
    	 System.out.println("time1:"+time1);

结果:

time1:20:15:36

3.6.2 parse(CharSequence text, DateTimeFormatter formatter)方法

用法:public static LocalTime parse(CharSequence text, DateTimeFormatter formatter)
参数:此方法接受两个参数text(即要解析的文本)和formatter(要使用的格式化程序)。
返回值:此方法返回LocalTime,它是解析的本地日期时间。
异常:如果无法解析文本,则此方法将引发DateTimeParseException。

示例:

LocalTime time2 = LocalTime.parse("10-10-10", DateTimeFormatter.ofPattern("HH-mm-ss"));
    	 System.out.println("time2:"+time2);

结果:

time2:10:10:10

3.7 时间转化为字符串

//时间转化为字符串
    	 LocalTime time3= LocalTime.parse("11-15-36", DateTimeFormatter.ofPattern("HH-mm-ss"));		 
    	 System.out.println("time3:"+time3.format(DateTimeFormatter.ofPattern("HH@mm@ss")));

结果:

time3:11@15@36

DateTimeFormatter.ofPattern("HH-mm-ss")中HH、mm、ss不能变,保证准确。

以下是关于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: - CalendarJava旧版的日期时间处理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、付费专栏及课程。

余额充值