Java新的时间日期类

来源:互联网 发布:九亿娱乐域名 编辑:程序博客网 时间:2024/06/05 10:27

一、LocalDate

LocalDate类使用ISO日历表示年、月、日。

LocalDate类的常用方法:

1、LocalDate.now():获取系统当前日期。

2、LocalDate.of(int year,int month,int dayOfMonth):按照指定日期创建LocalDate对象。

3、getYear():返回日期中的年份。

4、getMonth():返回日期中的月份。

5、getDayOfMonth():返回月份中的日。

 案例:用LocalDate获取当前日期。

[java] view plain copy
  1. package jax.timeanddate;  
  2.   
  3. import java.time.LocalDate;  
  4.   
  5. public class LocalDateTest {  
  6.   
  7.     public static void main(String[] args) {  
  8.         LocalDate date=LocalDate.now();  
  9.         System.out.print(date.getYear()+" 年 ");  
  10.         System.out.print(date.getMonthValue()+" 月 ");  
  11.         System.out.println(date.getDayOfMonth()+" 日 ");  
  12.         System.out.println(date.toString());  
  13.     }  
  14.   
  15. }  
输出:

2016 年 5 月 25 日 

2016-05-25


二、LocalTime

LocalTime类用于表示一天中的时间。

LocalTime类的常用方法:

1、LocalTime.now():获取系统当前时间。

2、LocalTime.of(int hour,int minute,int sec):按照指定时间创建LocalTime对象。

3、getHour():返回小时。

4、getMinute():返回分钟。

5、getSecond():返回秒。

 案例:用LocalTime获取当前时间。

[java] view plain copy
  1. package jax.timeanddate;  
  2.   
  3. import java.time.LocalTime;  
  4.   
  5. public class LocalTimeTest {  
  6.   
  7.     public static void main(String[] args) {  
  8.         LocalTime time=LocalTime.now();  
  9.         System.out.print(time.getHour()+" 时 ");  
  10.         System.out.print(time.getMinute()+" 分 ");  
  11.         System.out.println(time.getSecond()+" 秒 ");  
  12.         System.out.println(time.toString());  
  13.     }  
  14.   
  15. }  
输出:

15 时 43 分 3 秒 

15:43:03.432


三、LocalDateTime

LocalDateTime类用于表示日期和时间。

LocalDateTime类的常用方法:

1、LocalDateTime.now():获取系统当前日期和时间。

2、LocalDateTime.of(int year,int month,int dayOfMonth,int hour,int minute,int sec):按照指定时间创建LocalDateTime对象。

3、getYear():返回日期中的年份。

4、getMonth():返回日期中的月份。

5、getDayOfMonth():返回月份中的日。

6、getHour():返回小时。

7、getMinute():返回分钟。

8、getSecond():返回秒。

 案例:用LocalDateTime获取当前时间。

[java] view plain copy
  1. package jax.timeanddate;  
  2.   
  3. import java.time.LocalDateTime;  
  4.   
  5. public class LocalDateTimeTest {  
  6.   
  7.     public static void main(String[] args) {  
  8.         LocalDateTime time=LocalDateTime.now();  
  9.         System.out.print(time.getYear()+" 年 ");  
  10.         System.out.print(time.getMonthValue()+" 月 ");  
  11.         System.out.print(time.getDayOfMonth()+" 日 ");  
  12.         System.out.print(time.getHour()+" 时 ");  
  13.         System.out.print(time.getMinute()+" 分 ");  
  14.         System.out.println(time.getSecond()+" 秒 ");  
  15.         System.out.println(time.toString());  
  16.     }  
  17.   
  18. }  
输出:

2016 年 5 月 25 日 15 时 48 分 12 秒 

2016-05-25T15:48:12.526


四、DateTimeFormatter
DateTimeFormatter类用于将字符串解析为日期

常用方法:

1、static ofPattern(String pattern):作用:按照pattern字符串指定的格式创建DateTimeFormmter对象

2、LocalDateTime.parse(strDate,formatter);

案例:将字符串“2016-05-25 15:53:55”格式化为一个LocalDateTime类的对象,并显示年月日时分秒。

[java] view plain copy
  1. package jax.timeanddate;  
  2.   
  3. import java.time.LocalDateTime;  
  4. import java.time.format.DateTimeFormatter;  
  5.   
  6. public class DateTimeFormatterTest {  
  7.     public static void main(String[] args) {  
  8.           
  9.         DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");  
  10.         LocalDateTime time=LocalDateTime.parse("2016-05-25 15:53:55",df);  
  11.         System.out.print(time.getYear()+" 年 ");  
  12.         System.out.print(time.getMonthValue()+" 月 ");  
  13.         System.out.print(time.getDayOfMonth()+" 日 ");  
  14.         System.out.print(time.getHour()+" 时 ");  
  15.         System.out.print(time.getMinute()+" 分 ");  
  16.         System.out.println(time.getSecond()+" 秒 ");  
  17.         System.out.println(time.toString());  
  18.     }  
  19. }  

五、ZonedDateTime

ZonedDateTime处理日期和时间与相应的时区

1、ZonedDateTime.now():获取系统当前日期和时间。

2、String format(DateTimeFormatter formatter);按指定模版将日期对象格式化为字符串

案例:将当前日期格式化为字符串,并显示年月日时分秒。

[java] view plain copy
  1. package jax.timeanddate;  
  2.   
  3. import java.time.ZonedDateTime;  
  4. import java.time.format.DateTimeFormatter;  
  5.   
  6. public class ZonedDateTimeTest {  
  7.     public static void main(String[] args) {  
  8.         ZonedDateTime time=ZonedDateTime.now();  
  9.         DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");  
  10.         System.out.println(time.format(df));  
  11.     }  
  12. }  
输出:

2016/05/25 16:02:22


import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;


public class Test3 {
public static void main(String[] args) {
 DateTimeFormatter df=DateTimeFormatter.ofPattern("YYYY-MM-dd hh:mm:ss");
 Date date=new Date();
 Instant instant = date.toInstant();
 ZoneId zoneId = ZoneId.systemDefault();
 LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
 String format = df.format(localDateTime);
 System.out.println(format);
 
 ZonedDateTime zdt=ZonedDateTime.now();
 LocalDateTime localDateTime2 = zdt.toLocalDateTime();
 String format2 = df.format(localDateTime2);
 System.out.println(format2);
}
}

输出:

2017-10-12 02:47:18
2017-10-12 02:47:18


原创粉丝点击