您的位置:首页 > 编程语言 > Java开发

Java8新特性——LocalDate

2016-06-24 18:19 429 查看

LocalDate类使用ISO日历表示年,月,日

LocalDate.now():

获取系统当前日期 :2016-06-24

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

按指定日期创建LocalDate对象。

LocalDate date=LocalDate.of(2015,5,30); 2015-05-30

date.getYear(); //返回当前日期年

date.getMonthValue(); //返回当前日期月份

date.getDayOfMonth(); //返回当前日期天

LocalDate dateNow = LocalDate.now();
LocalDate dateTemp = LocalDate.of(2015, 6, 30);
int year = dateNow.getYear();//返回当前日期年
int monthValue = dateNow.getMonthValue();//返回当前日期月份
int dayOfMonth = dateNow.getDayOfMonth();//返回当前日期天


LocalTiem类用于表示一天中的时间

LocalTime.now()

获得当前系统的时间:17:47:22.905

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

按指定时间创建LocalTime对象

getHour()

getMinute()

getSecond()

LocalDateTime类用于表示日期和时间

LocalDateTime类的常用方法:

LocalDateTime.now():获取系统当前时间。

LocalDateTime.of(int year,int month,int dayOfMonth

int hour,int minute,int second)

按指定日期和时间创建LocalDateTime对象。

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

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

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

getHour():返回小时。

getMinute():返回分钟。

getSecond():返回秒。

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

常用方法:

1、static ofPattern(String pattern);

作用:按pattern字符串指定的格式创建DateTimeFormatter对象。

DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);

2、 LocalDateTime.parse(strDate, formatter);

作用:按指定日期模板和该模版字符串创建LocalDateTime对象。

DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time=LocalDateTime.parse("2016-06-23 18:12:30",formatter);
System.out.println(time);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 日历 新特性