Java8中时间的字符串和Long时间戳互转

1、取当前时间戳

    Long millisecond = Instant.now().toEpochMilli();  // 精确到毫秒
    Long second = Instant.now().getEpochSecond();// 精确到秒

2、将Long类型的时间戳转成字符串

/**
     * 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss
     */
    public static String timeToString(Long time){
        Assert.notNull(time, "time is null");
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));
    }

3、时间字符串转成Long类型的时间戳

/**
     * 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
     */
    public static Long timeToLong(String time) { 
        Assert.notNull(time, "time is null");
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", ftf);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    } 

4、时间转换Utils

            /**
         * 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss
         */
        public static String timeToString(Long time, String format){
            Assert.notNull(time, "time is null");
            // "yyyy-MM-dd HH:mm:ss"
            DateTimeFormatter formatString = DateTimeFormatter.ofPattern(format);
            return formatString.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
        }
        /**
         * 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
         */
        public static Long timeToLong(String time) {
            Assert.notNull(time, "time is null");
            DateTimeFormatter formatString = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", formatString);
            return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        }
        /**
         * localDate 格式化
         */
        public static String localDateFormat(LocalDate localDate, String format){
            Assert.notNull(localDate, "time is null");
            // "yyyy-MM-dd HH:mm:ss"
            String localDateStr = DateTranslate.timeToString(localDate2Second(localDate), format);
            return localDateStr;
        }
        /**
         * localDate 格式化
         */
        public static String localDateTimeFormat(LocalDateTime localDateTime, String format){
            Assert.notNull(localDateTime, "time is null");
            // "yyyy-MM-dd HH:mm:ss"
            String localDateTimeStr = DateTranslate.timeToString(localDateTime2Second(localDateTime), format);
            return localDateTimeStr;
        }
        /**
         * 取本月第一天
         */
        public static LocalDate firstDayOfThisMonth() {
            LocalDate today = LocalDate.now();
            return today.with(TemporalAdjusters.firstDayOfMonth());
        }
        /**
         * 取本月第一天
         */
        public static LocalDate firstDayOfLastMonth() {
            LocalDate today = LocalDate.now();
            today = today.minusMonths(1);
            return today.with(TemporalAdjusters.firstDayOfMonth());
        }
        /**
         * 取本月第N天
         */
        public static LocalDate dayOfThisMonth(int n) {
            LocalDate today = LocalDate.now();
            return today.withDayOfMonth(n);
        }
        /**
         * 取本月最后一天
         */
        public static LocalDate lastDayOfThisMonth() {
            LocalDate today = LocalDate.now();
            return today.with(TemporalAdjusters.lastDayOfMonth());
        }
        /**
         * 获取本周一
         */
        public static LocalDateTime firstDayOfWeek(Long date) {
            // long转LocalDateTime
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
            return localDateTime.with(DayOfWeek.MONDAY);
        }
        /**
         * 获取上周一
         */
        public static LocalDateTime firstDayOfLastWeek(Long date) {
            // long转LocalDateTime
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
            LocalDateTime localDateTime1 = localDateTime.plusDays(-7);
            return localDateTime1.with(DayOfWeek.MONDAY);
        }
        /**
         * 获取上周一
         */
        public static LocalDateTime lastDay(Long date) {
            // long转LocalDateTime
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(date), ZoneId.systemDefault());
            LocalDateTime localDateTime1 = localDateTime.plusDays(-1);
            return localDateTime1;
        }
        /**
         * 取本月第一天的开始时间
         */
        public static LocalDateTime startOfThisMonth() {
            return LocalDateTime.of(firstDayOfThisMonth(), LocalTime.MIN);
        }
        /**
         * 取本月最后一天的结束时间
         */
        public static LocalDateTime endOfThisMonth() {
            return LocalDateTime.of(lastDayOfThisMonth(), LocalTime.MAX);
        }
        /** LocalDate转时间戳 */
        public static Long localDate2Second(LocalDate localDate) {
            return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
        }
        /** LocalDate转时间戳 */
        public static Long localDateTime2Second(LocalDateTime localDateTime) {
            return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
        }

java 8 Localdate常用API

====================================正确答案==================================
getYear()    int    获取当前日期的年份
getMonth()    Month    获取当前日期的月份对象
getMonthValue()    int    获取当前日期是第几月
getDayOfWeek()    DayOfWeek    表示该对象表示的日期是星期几
getDayOfMonth()    int    表示该对象表示的日期是这个月第几天
getDayOfYear()    int    表示该对象表示的日期是今年第几天
withYear(int year)    LocalDate    修改当前对象的年份
withMonth(int month)    LocalDate    修改当前对象的月份
withDayOfMonth(int dayOfMonth)    LocalDate    修改当前对象在当月的日期
isLeapYear()    boolean    是否是闰年
lengthOfMonth()    int    这个月有多少天
lengthOfYear()    int    该对象表示的年份有多少天(365或者366)
plusYears(long yearsToAdd)    LocalDate    当前对象增加指定的年份数
plusMonths(long monthsToAdd)    LocalDate    当前对象增加指定的月份数
plusWeeks(long weeksToAdd)    LocalDate    当前对象增加指定的周数
plusDays(long daysToAdd)    LocalDate    当前对象增加指定的天数
minusYears(long yearsToSubtract)    LocalDate    当前对象减去指定的年数
minusMonths(long monthsToSubtract)    LocalDate    当前对象减去注定的月数
minusWeeks(long weeksToSubtract)    LocalDate    当前对象减去指定的周数
minusDays(long daysToSubtract)    LocalDate    当前对象减去指定的天数
compareTo(ChronoLocalDate other)    int    比较当前对象和other对象在时间上的大小,返回值如果为正,则当前对象时间较晚,
isBefore(ChronoLocalDate other)    boolean    比较当前对象日期是否在other对象日期之前
isAfter(ChronoLocalDate other)    boolean    比较当前对象日期是否在other对象日期之后
isEqual(ChronoLocalDate other)    boolean    比较两个日期对象是否相等
故乡明
原文地址:https://www.cnblogs.com/luweiweicode/p/14217505.html