jdk1.8的新特性——时间日期之LocalDateTime

在上一篇jdk1.8的新特性——时间日期之LocalTime中,我们学习了LocalTime的API使用,其中更加全面得讲解了如何创建一个LocalTime对象,并且初步学会了DateTimeFormatter的使用,将时间的格式转化成我们想要的字符表达式。同时,我们结合LocalDate对象,在LocalTime对象的基础上“合成”了一个LocalDateTime对象。那么这个LocalDateTime对象有哪些方法需要我们掌握,现在就让我们开始LocalDateTime的学习吧。:D

首先,如何获取或者创建一个LocalDateTime对象,常见的方法有:

 public static void main(String[] args) {

        //获取当前日期时间对象:2019-08-13T10:15:20,withNano(0)去掉毫秒数
        LocalDateTime localDateTime = LocalDateTime.now().withNano(0);

        //获取日期对象:2019-08-13
        LocalDate today = LocalDate.now();
        //创建时间对象:10:15:20
        LocalTime now = LocalTime.of(10, 15, 20);
        //创建LocalDateTime对象:2019-08-13T10:15:20
        LocalDateTime dateTime = LocalDateTime.of(today, now);

        //定义日期时间格式:yyyy年MM月dd日HH点mm分ss秒
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日HH点mm分ss秒");
        //新中国成立的日期时间对象:1949-10-01T14:00
        LocalDateTime birthdayOfPRC = LocalDateTime.parse("1949年10月01日14点00分00秒", dateTimeFormatter);
    }

这些方法都在上一篇中有过介绍,上一篇的最后我们定义过DateTimeFormmatter,然后根据LocalTime的实例方法format,将时间进行了格式化。那么,格式化后的字符能不能重新变成一个LocalTime对象呢?答案是肯定的,在上面代码中parse方法就是将一个日期时间字符按照给定的dateTimeFormatter格式进行解析,当然这个字符串要符合这个dateTimeFormatter格式。

接下来,我们看看from方法如何获取一个LocalDateTime对象。这也是我们上一篇遗留的一个知识点。在学习这个方法之前,我们需要先看看它的源代码,不然就是一头雾水。

/**
     * Obtains an instance of {@code LocalDateTime} from a temporal object.
     * <p>
     * This obtains an offset time based on the specified temporal.
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
     * which this factory converts to an instance of {@code LocalDateTime}.
     * <p>
     * The conversion extracts and combines the {@code LocalDate} and the
     * {@code LocalTime} from the temporal object.
     * Implementations are permitted to perform optimizations such as accessing
     * those fields that are equivalent to the relevant objects.
     * <p>
     * This method matches the signature of the functional interface {@link TemporalQuery}
     * allowing it to be used as a query via method reference, {@code LocalDateTime::from}.
     *
     * @param temporal  the temporal object to convert, not null
     * @return the local date-time, not null
     * @throws DateTimeException if unable to convert to a {@code LocalDateTime}
     */
    public static LocalDateTime from(TemporalAccessor temporal) {
        if (temporal instanceof LocalDateTime) {
            return (LocalDateTime) temporal;
        } else if (temporal instanceof ZonedDateTime) {
            return ((ZonedDateTime) temporal).toLocalDateTime();
        } else if (temporal instanceof OffsetDateTime) {
            return ((OffsetDateTime) temporal).toLocalDateTime();
        }
        try {
            LocalDate date = LocalDate.from(temporal);
            LocalTime time = LocalTime.from(temporal);
            return new LocalDateTime(date, time);
        } catch (DateTimeException ex) {
            throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " +
                    temporal + " of type " + temporal.getClass().getName(), ex);
        }
    }

这个方法我们直接读代码,就不翻译注释了。方法参数是一个TemporalAccessor对象(TemporalAccessor:时间访问器),从源码中可以知道TemporalAccessor是个接口,那既然是接口,这个temporal对象就应该是实现类的对象(关于时间、日期、日期时间、瞬时、时区日期时间等的类都实现TemporalAccessor)。从代码中可以看出,程序先对这个temporal对象进行instanceof判断,也就是说先判断temporal是不是LocalDateTime、ZonedDateTime、OffsetDateTime三者中的一个实例。如果是,直接强制转换后使用toLocalDateTime方法。如果不是三者之一的实例,就从这个temporal中获取LocalDate的date对象和LocalTime的time对象,再new一个LocalDateTime对象返回。这个地方我们可以看到上一篇中没有讲解得LocalTime的from办法,我们可以继续进入这个from方法看看。

public static LocalTime from(TemporalAccessor temporal) {
        Objects.requireNonNull(temporal, "temporal");
        LocalTime time = temporal.query(TemporalQueries.localTime());
        if (time == null) {
            throw new DateTimeException("Unable to obtain LocalTime from TemporalAccessor: " +
                    temporal + " of type " + temporal.getClass().getName());
        }
        return time;
    }

可以看到"LocalTime time = temporal.query(TemporalQueries.localTime());" 这句代码是从temporal中查询出LocalTime对象,那我们现在还要不要继续跟进这个方法呢?答案在于你自己。:D

2019/08/24更

我这样写到底对不对,该不该花那么大力气去读源码,去理解。或是找个例子看看,知道怎么用就行了。那样无疑很快,很高效,这种成熟的API有没有必要去读源码。读源码的时候,耗时是肯定的,有些单词不认识就需要查,有些不懂的地方就要去思考。不过,收获也是肯定比较多,比如怎么更好的设计接口,抽象类。源码就好比作文中的范文一样,无论注释,方法命名,还是访问控制符的设计,各个关键字用得都很精准严谨。对我编程有很大的启迪作用,可是目前的国内环境(除了大厂的一些部门)就是不重视代码质量,在部门中我一个人重视感觉力不从心,领导甚至说我没有包容性,让我学会兼容,兼容那些不规范。我也很无奈,只能说自上而下的领导不重视长远发展,只顾眼前。但是如果完全的融入他们,我都瞧不起自己个儿。明知不对,还去做,那我成什么人了。人生就是充满着矛盾,我改变不了他们,也不想变成他们那样。那么,我想只有更好充实自己,然后寻找与我价值观相近的公司。

大家可以从这个网站上继续学习 https://www.yiibai.com/javatime

还有这篇博客也不错 https://blog.csdn.net/leolu007/article/details/53112363

如果有人想看更深入的内容,可以给我留言。我会考虑是否继续深入地写下去,再见!:D

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值