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;

/**
 * LocalDateTime常用操作工具类,主要包括的工具如下:
 * - LocalDateTime转为Date    localDateTime2Date()
 * - Date转为LocalDateTime    date2LocalDateTime()
 * - LocalDateTime转为String(yyyy-MM-dd HH:mm:ss)    localDateTime2String()
 * - LocalDateTime转为String    localDateTime2String()
 * - String转为LocalDateTime    string2LocalDateTime()
 * - LocalDateTime转为Unix时间戳    localDateTime2Timestamp()
 * - Unix时间戳转为LocalDateTime    timestamp2LocalDateTime()
 * - LocalDateTime转为Java时间戳(TimeMillis)    localDateTime2TimeMillis()
 * - Java时间戳(TimeMillis)转为LocalDateTime    timeMillis2LocalDateTime()
 * - 获取年的开始时间    getYearStart()
 * - 获取某年的开始时间    getYearStart()
 * - 获取年的结束时间    getYearEnd()
 * - 获取某年的结束时间    getYearEnd()
 * - 获取月的开始时间    getMonthStart()
 * - 获取某月的开始时间    getMonthStart()
 * - 获取月的结束时间    getMonthEnd()
 * - 获取某月的结束时间    getMonthEnd()
 * - 获取日的开始时间    getDayStart()
 * - 获取某日的开始时间    getDayStart()
 * - 获取日的结束时间    getDayEnd()
 * - 获取某日的结束时间    getDayEnd()
 */
public class LocalDateTimeUtils {

    /**
     * 时间格式
     * DATE_FORMAT_NORMAL: yyyy-MM-dd HH:mm:ss
     * DATE_FORMAT_NUMBER: yyyyMMddHHmmss
     * DATE_FORMAT_T: yyyy-MM-ddTHH:mm:ss
     */
    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";

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

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

    /**
     * LocalDateTime转为String(yyyy-MM-dd HH:mm:ss)
     */
    public static String localDateTime2String(LocalDateTime localDateTime) {
        return localDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT_NORMAL));
    }

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

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

    /**
     * LocalDateTime转为Unix时间戳
     */
    public static long localDateTime2Timestamp(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    /**
     * Unix时间戳转为LocalDateTime
     */
    public static LocalDateTime timestamp2LocalDateTime(long timestamp) {
        return LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
    }

    /**
     * LocalDateTime转为Java时间戳(TimeMillis)
     */
    public static long localDateTime2TimeMillis(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * Java时间戳(TimeMillis)转为LocalDateTime
     */
    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);
    }

    /**
     * 获取某年的开始时间
     * @param offset 0 本年,1 明年,-1 去年,以此类推
     */
    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);
    }

    /**
     * 获取某年的结束时间
     * @param offset 0 本年,1 明年,-1 去年,以此类推
     */
    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);
    }

    /**
     * 获取某月的开始时间
     * @param offset 0 本月,1 下月,-1 上月,以此类推
     */
    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);
    }

    /**
     * 获取月的结束时间
     * @param offset 0 本月,1 下月,-1 上月,以此类推
     */
    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);
    }

    /**
     * 获取某日的开始时间
     * @param offset 0 今日,1 明日,-1 昨日,以此类推
     */
    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);
    }

    /**
     * 获取日的结束时间
     * @param offset 0 今日,1 明日,-1 昨日,以此类推
     */
    public static LocalDateTime getDayEnd(int offset) {
        return LocalDateTime.now().plusDays(offset).with(LocalTime.MAX);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值