LocalDateTime、LocalDate

本文详细介绍了Java 8中新的时间日期API,包括LocalTime、LocalDate和LocalDateTime的使用方法,展示了如何创建、解析及格式化时间日期,并提供了Controller层的配置示例。

前言

博主github
博主个人博客http://blog.healerjean.com

1、LocalTime

//LocalTime包含毫秒
LocalTime now = LocalTime.now(); // 11:09:09.240

//清除毫秒数
LocalTime now = LocalTime.now().withNano(0)); // 11:09:09

//构造时间
LocalTime zero = LocalTime.of(0, 0, 0); // 00:00:00
LocalTime mid = LocalTime.parse("12:00:00"); // 12:00:00

2、LocalDate

2.1、取当前日期:
LocalDate today = LocalDate.now(); 
2.2、根据年月日取日期,5月就是05:>2014-12-25
LocalDate date= LocalDate.of(2017, 05, 25); 
2.3、 根据字符串取: 严格按照ISO yyyy-MM-dd验证,02写成2都不行,当然也有一个重载方法允许自己定义格式
LocalDate date= LocalDate.parse("2014-02-28"); //正确

LocalDate.parse("2014-02-29"); // 无效日期无法通过:

3、LocalDateTime

3.1、2013-12-31 23:59
LocalDateTime d1 = LocalDateTime.of(2013, 12, 31, 23, 59);  
3.2、年月日 时分秒 纳秒
LocalDateTime d2 = LocalDateTime.of(2013, 12, 31, 23, 59,59, 11);  
3.3、解析String—>LocalDateTime
3.3.1、2013-12-31T23:59
LocalDateTime d4 = LocalDateTime.parse("2013-12-31T23:59");  
3.3.2、2013-12-31T23:59:59.999
LocalDateTime d5 = LocalDateTime.parse("2013-12-31T23:59:59.999");
3.3.3、标准String日期 转化为LocalDateTime
  
    private LocalDateTime getLocalDateTime(String strDate) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = formatter.parse(strDate);
            Instant instant = Instant.ofEpochMilli(date.getTime());
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            return localDateTime;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  null;
    }

4、Contorller中使用

4.1、Local类型的日期
@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
    });
    binder.registerCustomEditor(LocalDateTime.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
    });
    binder.registerCustomEditor(LocalTime.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(LocalTime.parse(text, DateTimeFormatter.ofPattern("HH:mm:ss")));
        }
    });
}
4.2、普通date

    @InitBinder
    public void dateBinder(WebDataBinder binder){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
    }

!ContactAuthor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值