美文网首页
LocalDateTime使用

LocalDateTime使用

作者: 不知不怪 | 来源:发表于2020-03-11 14:56 被阅读0次
package com.zhxd.common.utils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @类说明 日历相关工具
 * @author 高振中
 * @创建时间 2020年01月11日
 */
public class DateUtils {
    static final DateTimeFormatter DATETIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyyMMdd");
    static final ZoneId ZONEID = ZoneId.systemDefault();

    /**
     * @方法说明 格式化日期为 yyyy-MM-dd HH:mm:ss 返回字符串
     **/
    public static String dateTime(Date date) {
        return date.toInstant().atZone(ZONEID).toLocalDateTime().format(DATETIME);
    }

    /**
     * @方法说明 取当前日期 yyyyMMdd 返回字符串
     **/
    public static String nowDate() {
        return LocalDateTime.now().atZone(ZONEID).format(DATE);
    }

    /**
     * @方法说明 把字符串(yyyy-MM-dd HH:mm:ss)转成日期 返回DATE
     **/
    public static Date StringToDate(String date) {
        LocalDateTime localDateTime = LocalDateTime.parse(date, DATETIME);
        return Date.from(localDateTime.atZone(ZONEID).toInstant());
    }

    /**
     * @方法说明 获取当前时间
     */
    public static Date getNowTime() {
        return Date.from(LocalDateTime.now().atZone(ZONEID).toInstant());
    }

    /**
     * @方法说明 获取指定时间和现在的时间差分钟数
     */
    public static long getMinute(Date date) {
        return (new Date().getTime() - date.getTime()) / (1000 * 60);
    }
}

相关文章

网友评论

      本文标题:LocalDateTime使用

      本文链接:https://www.haomeiwen.com/subject/jfjcjhtx.html