LocalDateTime小记

今天在使用LocalDateTime时需要将时间转化使劲啊戳,原来我也没怎么用过.

发现又一个方法

    default long toEpochSecond(ZoneOffset offset) {
        Objects.requireNonNull(offset, "offset");
        long epochDay = toLocalDate().toEpochDay();
        long secs = epochDay * 86400 + toLocalTime().toSecondOfDay();
        secs -= offset.getTotalSeconds();
        return secs;
    }

这个方法需要传入一个ZoneOffset的对象,经过查询资料是这么做的

    public static Long localDateTime2LongSecond(LocalDateTime localDateTime) {
        //获得当前时区
        Instant instant = Instant.now();
        ZoneId systemZone = ZoneId.systemDefault();
        ZoneOffset currentOffsetForMyZone =  
        systemZone.getRules().getOffset(instant);
        return localDateTime.toEpochSecond(currentOffsetForMyZone);
    }

然而,我自己测试的时候发现该方法返回的时间戳和Date的时间戳有1000倍的出入

1570712734
1570712734225

发现LocalDateTime的toEpochSecond方法返回的单位是秒,而Date.getTime()返回的单位是毫秒.

源码的注释也有说明只是我给漏掉了,第一行就说了是seconds

    /**
     * Converts this date-time to the number of seconds from the epoch
     * of 1970-01-01T00:00:00Z.
     * <p>
     * This combines this local date-time and the specified offset to calculate the
     * epoch-second value, which is the number of elapsed seconds from 1970-01-01T00:00:00Z.
     * Instants on the time-line after the epoch are positive, earlier are negative.
     * <p>
     * This default implementation calculates from the epoch-day of the date and the
     * second-of-day of the time.
     *
     * @param offset  the offset to use for the conversion, not null
     * @return the number of seconds from the epoch of 1970-01-01T00:00:00Z
     */
    default long toEpochSecond(ZoneOffset offset) {
        Objects.requireNonNull(offset, "offset");
        long epochDay = toLocalDate().toEpochDay();
        long secs = epochDay * 86400 + toLocalTime().toSecondOfDay();
        secs -= offset.getTotalSeconds();
        return secs;
    }

真正的实现方式是

/**
     * @description: 有LocalDateTime转换成时间戳(毫秒 )
     * @param: [localDateTime]
     * @return: java.lang.Long
     * @author: lky
     * @date: 2019/10/10 09:57
     */
    public static Long localDateTime2Long(LocalDateTime localDateTime) {
        //获得当前时区
        Instant instant = Instant.now();
        ZoneId systemZone = ZoneId.systemDefault();
        ZoneOffset currentOffsetForMyZone = systemZone.getRules().getOffset(instant);
        return localDateTime.atZone(currentOffsetForMyZone).toInstant().toEpochMilli();
    }

另送时间戳转换为LocalDateTime的方法,*1000的操作是为了将(秒)转换为毫秒 ,请忽略

/**
     * @description: 将Long类型的时间戳转换为LocalDateTime类型
     * @param: [timestamp]
     * @return: java.time.LocalDateTime
     * @author: lky
     * @date: 2019/10/9 11:08
     */
    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        if ((timestamp + "").length() < 13) {
            timestamp *= 1000;
        }
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值