Java8新特性——LocalDate

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

  1. LocalDate.now():
    获取系统当前日期 :2016-06-24

  2. LocalDate.of(int year,int month,int dayofMonth)
    按指定日期创建LocalDate对象。
    LocalDate date=LocalDate.of(2015,5,30); 2015-05-30

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

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

  5. 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类用于表示一天中的时间

  1. LocalTime.now()
    获得当前系统的时间:17:47:22.905

  2. LocalTime.of(int hour,int minute,int second)
    按指定时间创建LocalTime对象

  3. getHour()

  4. getMinute()

  5. getSecond()


LocalDateTime类用于表示日期和时间

LocalDateTime类的常用方法:
  1. LocalDateTime.now():获取系统当前时间。
  2. LocalDateTime.of(int year,int month,int dayOfMonth
    int hour,int minute,int second)
    按指定日期和时间创建LocalDateTime对象。
  3. getYear():返回日期中的年份。
  4. getMonth():返回日期中的月份。
  5. getDayOfMonth():返回月份中的日。
  6. getHour():返回小时。
  7. getMinute():返回分钟。
  8. 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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值