jdk1.8日期便捷处理类java.time.LocalDateTime

常用api

* now():静态方法,返回当前时间。
* of():静态方法,用于创建一个指定日期时间的对象。
* parse():静态方法,将字符串解析为日期时间对象。
* get():返回指定字段的值。
* with():返回一个被指定修改后的副本对象。
* plus():返回增加指定数量的时间后的副本对象。
* minus():返回减去指定数量的时间后的副本对象。
* isBefore():判断当前时间是否在指定时间之前。
* isAfter():判断当前时间是否在指定时间之后。
* isEqual():判断当前时间是否等于指定时间。
* toLocalDate():将当前日期时间转换为LocalDate类型。
* toLocalTime():将当前日期时间转换为LocalTime类型。
* toLocalDateTime():将当前日期时间转换为LocalDateTime类型。
* toInstant():将当前日期时间转换为格林威治时间(Instant类型)。
* format():将当前日期时间转换为指定格式的字符串。
* getMonth():返回月份信息。
* getDayOfWeek():返回星期信息。
* getDayOfMonth():返回月份中的第几天。
* getDayOfYear():返回年份中的第几天。
* withMonth():修改月份信息,返回一个新的日期时间对象。
* withDayOfMonth():修改月份中的天数信息,返回一个新的日期时间对象。
* withYear():修改年份信息,返回一个新的日期时间对象。
* toEpochSecond():将日期时间转换成秒数。
* from():从其他日期时间对象中获取当前对象的值。
* compareTo():用于比较两个日期时间对象的大小。

示例:

	//获取当前时间
    System.out.println(LocalDateTime.now());
    //获取当前时间时分秒
    System.out.println(LocalDateTime.now().toLocalTime());
    //获取当前时间年月日
  System.out.println(LocalDateTime.now().toLocalDate());
    //获取当前时间20天之前
    System.out.println(LocalDateTime.now().minusDays(20));
    //获取当前时间20天之后
    System.out.println(LocalDateTime.now().plusDays(20));
    //比较大小
    System.out.println(LocalDateTime.now().compareTo(LocalDateTime.now().plusDays(2)));   //-2
    //获取当天的开始时间
    LocalDateTime.now().toLocalDate().atStartOfDay()
    //获取次日的开始时间
   LocalDateTime.now().plusDays(1L).toLocalDate().atStartOfDay()
   //获取当天日期
    String toDay = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));

获取当日到次日的时间

		LocalDateTime now = LocalDateTime.now();
        LocalDateTime next = LocalDateTime.of(LocalDate.now().plusDays(1L), LocalTime.MIN);
        System.out.println("now:"+now);
        System.out.println("next:"+next);
        System.out.println(ChronoUnit.SECONDS.between(now, next));

LocalDateTime和时间戳互转

/**
     * 获取到毫秒级时间戳
     * @param localDateTime 具体时间
     * @return long 毫秒级时间戳
     */
    public static long toEpochMilli(LocalDateTime localDateTime){
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 毫秒级时间戳转 LocalDateTime
     * @param epochMilli 毫秒级时间戳
     * @return LocalDateTime
     */
    public static LocalDateTime ofEpochMilli(long epochMilli){
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneOffset.of("+8"));
    }

    /**
     * 获取到秒级时间戳
     * @param localDateTime 具体时间
     * @return long 秒级时间戳
     */
    public static long toEpochSecond(LocalDateTime localDateTime){
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 秒级时间戳转 LocalDateTime
     * @param epochSecond 秒级时间戳
     * @return LocalDateTime
     */
    public static LocalDateTime ofEpochSecond(long epochSecond){
        return LocalDateTime.ofEpochSecond(epochSecond, 0,ZoneOffset.of("+8"));
    }

LocalDateTime和Date互转

 /**
     * Date时间类转LocalDateTime
     * @param date Date时间类
     * @return LocalDateTime
     */
    public static LocalDateTime ofDate(Date date){
        return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
    }

    /**
     * LocalDateTime时间类转 Date时间类
     * @param localDateTime LocalDateTime时间类
     * @return Date时间类
     */
    public static Date toDate(LocalDateTime localDateTime){
        return Date.from(localDateTime.atZone(ZoneOffset.of("+8")).toInstant());
    }

LocalDateTime和字符串互转

 /**
     * LocalDateTime转时间格式字符串
     * @param localDateTime 时间
     * @return string 
     */
    public static String formatToString(LocalDateTime localDateTime){
        String format  = "yyyy:MM:dd HH:mm:ss";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(dateTimeFormatter);
    }

    /**
     *  时间字符串 转LocalDateTime
     * @param localDateTime  时间字符串
     * @return LocalDateTime
     */
    public static LocalDateTime stringToFormat(String localDateTime){
        String format  = "yyyy:MM:dd HH:mm:ss";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(localDateTime,dateTimeFormatter);
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值