栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java8时间工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java8时间工具类

java8时间工具类

package com.test.test.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;


public class LocalDateTimeUtils {

    
    public static final String DATE_FORMAT_NORMAL = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_NUMBER = "yyyyMMddHHmmss";
    public static final String DATE_FORMAT_T = "yyyy-MM-ddTHH:mm:ss";

    
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    
    public static LocalDateTime date2LocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    
    public static String localDateTime2String(LocalDateTime localDateTime) {
        return localDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT_NORMAL));
    }

    
    public static String localDateTime2String(LocalDateTime localDateTime, String datePattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(datePattern));
    }

    
    public static LocalDateTime string2LocalDateTime(String dateString, String datePattern) {
        return LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(datePattern));
    }

    
    public static long localDateTime2Timestamp(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    
    public static LocalDateTime timestamp2LocalDateTime(long timestamp) {
        return LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
    }

    
    public static long localDateTime2TimeMillis(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    
    public static LocalDateTime timeMillis2LocalDateTime(long timeMillis) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), ZoneId.systemDefault());
    }

    
    public static LocalDateTime getYearStart(LocalDateTime localDateTime) {
         return localDateTime.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
    }

    
    public static LocalDateTime getYearStart(int offset) {
         return LocalDateTime.now().plusYears(offset).with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
    }

    
    public static LocalDateTime getYearEnd(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
    }

    
    public static LocalDateTime getYearEnd(int offset) {
        return LocalDateTime.now().plusYears(offset).with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
    }

    
    public static LocalDateTime getMonthStart(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
    }

    
    public static LocalDateTime getMonthStart(int offset) {
        return LocalDateTime.now().plusMonths(offset).with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
    }

    
    public static LocalDateTime getMonthEnd(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
    }

    
    public static LocalDateTime getMonthEnd(int offset) {
        return LocalDateTime.now().plusMonths(offset).with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
    }

    
    public static LocalDateTime getDayStart(LocalDateTime localDateTime) {
        return localDateTime.with(LocalTime.MIN);
    }

    
    public static LocalDateTime getDayStart(int offset) {
        return LocalDateTime.now().plusDays(offset).with(LocalTime.MIN);
    }

    
    public static LocalDateTime getDayEnd(LocalDateTime localDateTime) {
        return localDateTime.with(LocalTime.MAX);
    }

    
    public static LocalDateTime getDayEnd(int offset) {
        return LocalDateTime.now().plusDays(offset).with(LocalTime.MAX);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020810.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号