当年日期当月日期当周日期的第一天最后一天的各种时间格式的工具类

package com.example.yunCost.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

public class TimeUtils {

    // 获取当前日期时间
    public static Date getCurrentDateTime() {
        LocalDateTime now = LocalDateTime.now();
        return convertLocalDateTimeToDate(now);
    }

    // 获取当前日期
    public static Date getCurrentDate() {
        LocalDate now = LocalDate.now();
        return convertLocalDateToDate(now);
    }

    // 获取当前时间
    public static Date getCurrentTime() {
        LocalTime now = LocalTime.now();
        return convertLocalTimeToDate(now);
    }





    // 获取当年的第一天
    public static Date getFirstDayOfYear() {
        LocalDate firstDayOfYear = Year.now().atMonth(Month.JANUARY).atDay(1);
        return convertLocalDateToDate(firstDayOfYear);
    }
    // 获取当年的最后一天
    public static Date getLastDayOfYear() {
        LocalDate lastDayOfYear = Year.now().atMonth(Month.DECEMBER).atEndOfMonth();
        return convertLocalDateToDate(lastDayOfYear);
    }
    // 获取当年的第一天加上 00:00:00 日期格式
    public static Date getFirstDayOfYearAddDate() {
        LocalDate firstDayOfYear = Year.now().atMonth(Month.JANUARY).atDay(1);
        LocalDateTime endOfDay = firstDayOfYear.atTime(00, 00, 00);
        return convertLocalDateTimeToDate(endOfDay);
    }
    // 获取当年的最后一天 加上23:59:59 日期格式
    public static Date getLastDayOfYearAddDate() {
        LocalDate lastDayOfYear = Year.now().atMonth(Month.DECEMBER).atEndOfMonth();
        LocalDateTime endOfDay = lastDayOfYear.atTime(23, 59, 59);
        return convertLocalDateTimeToDate(endOfDay);
    }
    // 获取当年的第一天加上 00:00:00 字符串格式
    public static String getFirstDayOfYearAddString() {
        LocalDate firstDayOfYear = Year.now().atMonth(Month.JANUARY).atDay(1);
        LocalDateTime endOfDay = firstDayOfYear.atTime(00, 00, 00);
        return formatDateTime(endOfDay, "yyyy-MM-dd HH:mm:ss");
    }
    // 获取当年的最后一天 加上23:59:59 字符串格式
    public static String getLastDayOfYearAddString() {
        LocalDate lastDayOfYear = Year.now().atMonth(Month.DECEMBER).atEndOfMonth();
        LocalDateTime endOfDay = lastDayOfYear.atTime(23, 59, 59);
        return formatDateTime(endOfDay, "yyyy-MM-dd HH:mm:ss");
    }





    // 获取当月的第一天
    public static Date getFirstDayOfMonth() {
        LocalDate firstDayOfMonth = LocalDate.now().withDayOfMonth(1);
        return convertLocalDateToDate(firstDayOfMonth);
    }
    // 获取当月的最后一天
    public static Date getLastDayOfMonth() {
        LocalDate lastDayOfMonth = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        return convertLocalDateToDate(lastDayOfMonth);
    }
    // 获取当月的第一天加上 日期
    public static Date getFirstDayOfMonthAddDate() {
        LocalDate firstDayOfMonth = LocalDate.now().withDayOfMonth(1);
        LocalDateTime endOfDay = firstDayOfMonth.atTime(00, 00, 00);
        return convertLocalDateTimeToDate(endOfDay);
    }
    // 获取当月的最后一天加上 日期
    public static Date getLastDayOfMonthAddDate() {
        LocalDate lastDayOfMonth = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        LocalDateTime endOfDay = lastDayOfMonth.atTime(23, 59, 59);
        return convertLocalDateTimeToDate(endOfDay);
    }
    // 获取当月的第一天加上 字符串
    public static String getFirstDayOfMonthAddString() {
        LocalDate firstDayOfMonth = LocalDate.now().withDayOfMonth(1);
        LocalDateTime endOfDay = firstDayOfMonth.atTime(00, 00, 00);
        return formatDateTime(endOfDay, "yyyy-MM-dd HH:mm:ss");
    }
    // 获取当月的最后一天加上 字符串
    public static String getLastDayOfMonthAddString() {
        LocalDate lastDayOfMonth = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        LocalDateTime endOfDay = lastDayOfMonth.atTime(23, 59, 59);
        return formatDateTime(endOfDay, "yyyy-MM-dd HH:mm:ss");
    }





    // 获取当周的第一天
    public static Date getFirstDayOfWeek() {
        LocalDate firstDayOfWeek = LocalDate.now().with(ChronoField.DAY_OF_WEEK, 1);
        return convertLocalDateToDate(firstDayOfWeek);
    }
    // 获取当周的最后一天
    public static Date getLastDayOfWeek() {
        LocalDate lastDayOfWeek = LocalDate.now().with(ChronoField.DAY_OF_WEEK, 7);
        return convertLocalDateToDate(lastDayOfWeek);
    }
    // 获取当周的第一天加上 日期
    public static Date getFirstDayOfWeekAddDate() {
        LocalDate firstDayOfWeek = LocalDate.now().with(ChronoField.DAY_OF_WEEK, 1);
        LocalDateTime endOfDay = firstDayOfWeek.atTime(00, 00, 00);
        return convertLocalDateTimeToDate(endOfDay);
    }
    // 获取当周的最后一天加上 日期
    public static Date getLastDayOfWeekAddDate() {
        LocalDate lastDayOfWeek = LocalDate.now().with(ChronoField.DAY_OF_WEEK, 7);
        LocalDateTime endOfDay = lastDayOfWeek.atTime(23, 59, 59);
        return convertLocalDateTimeToDate(endOfDay);
    }
    // 获取当周的第一天加上 字符串
    public static String getFirstDayOfWeekAddString() {
        LocalDate firstDayOfWeek = LocalDate.now().with(ChronoField.DAY_OF_WEEK, 1);
        LocalDateTime endOfDay = firstDayOfWeek.atTime(00, 00, 00);
        return formatDateTime(endOfDay, "yyyy-MM-dd HH:mm:ss");
    }
    // 获取当周的最后一天加上 字符串
    public static String getLastDayOfWeekAddString() {
        LocalDate lastDayOfWeek = LocalDate.now().with(ChronoField.DAY_OF_WEEK, 7);
        LocalDateTime endOfDay = lastDayOfWeek.atTime(23, 59, 59);
        return formatDateTime(endOfDay, "yyyy-MM-dd HH:mm:ss");
    }


    // 获取当天加上 日期
    public static Date getFirstDayAddDate() {
        LocalDate firstDayOfWeek = LocalDate.now();
        LocalDateTime endOfDay = firstDayOfWeek.atTime(00, 00, 00);
        return convertLocalDateTimeToDate(endOfDay);
    }
    // 获取当天加上 日期
    public static Date getLastDayAddDate() {
        LocalDate lastDayOfWeek = LocalDate.now();
        LocalDateTime endOfDay = lastDayOfWeek.atTime(23, 59, 59);
        return convertLocalDateTimeToDate(endOfDay);
    }
    // 获取当天加上 字符串
    public static String getFirstDayAddString() {
        LocalDate firstDayOfWeek = LocalDate.now();
        LocalDateTime endOfDay = firstDayOfWeek.atTime(00, 00, 00);
        return formatDateTime(endOfDay, "yyyy-MM-dd HH:mm:ss");
    }
    // 获取当天加上 字符串
    public static String getLastDayAddString() {
        LocalDate lastDayOfWeek = LocalDate.now();
        LocalDateTime endOfDay = lastDayOfWeek.atTime(23, 59, 59);
        return formatDateTime(endOfDay, "yyyy-MM-dd HH:mm:ss");
    }



    // 格式化日期时间
    public static String formatDateTime(Date date, String pattern) {
        LocalDateTime localDateTime = convertDateToLocalDateTime(date);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }

    // 计算两个日期之间的天数差异
    public static long daysBetween(Date startDate, Date endDate) {
        LocalDate startLocalDate = convertDateToLocalDate(startDate);
        LocalDate endLocalDate = convertDateToLocalDate(endDate);
        return ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
    }

    // 检查某个年份是否是闰年
    public static boolean isLeapYear(int year) {
        return Year.of(year).isLeap();
    }

    // 在指定日期上添加指定天数
    public static Date addDays(Date date, long daysToAdd) {
        LocalDate localDate = convertDateToLocalDate(date);
        LocalDate newDate = localDate.plusDays(daysToAdd);
        return convertLocalDateToDate(newDate);
    }

    // 在指定日期上减去指定天数
    public static Date subtractDays(Date date, long daysToSubtract) {
        LocalDate localDate = convertDateToLocalDate(date);
        LocalDate newDate = localDate.minusDays(daysToSubtract);
        return convertLocalDateToDate(newDate);
    }

    // 将 LocalDateTime 转换为 Date
    public static Date convertLocalDateTimeToDate(LocalDateTime localDateTime) {
        long millis = localDateTime.atZone(java.time.ZoneId.systemDefault()).toInstant().toEpochMilli();
        return new Date(millis - (millis % 1000));
    }

    // 将 LocalDate 转换为 Date
    public static Date convertLocalDateToDate(LocalDate localDate) {
        return java.sql.Date.valueOf(localDate);
    }

    // 将 LocalTime 转换为 Date
    public static Date convertLocalTimeToDate(LocalTime localTime) {
        return java.sql.Time.valueOf(localTime);
    }

    // 将 Date 转换为 LocalDateTime
    public static LocalDateTime convertDateToLocalDateTime(Date date) {
        return new java.sql.Timestamp(date.getTime()).toLocalDateTime();
    }

    // 将 Date 转换为 LocalDate
    public static LocalDate convertDateToLocalDate(Date date) {
        return new java.sql.Date(date.getTime()).toLocalDate();
    }

    // 将 Date 转换为 LocalTime
    public static LocalTime convertDateToLocalTime(Date date) {
        return new java.sql.Time(date.getTime()).toLocalTime();
    }

    // 格式化日期时间
    public static String formatDateTime(LocalDateTime dateTime, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return dateTime.format(formatter);
    }


    public static void main(String[] args) {
        // 获取当前日期时间
        Date currentDateTime = getCurrentDateTime();
        System.out.println("当前日期时间:" + currentDateTime);

        // 获取当前日期
        Date currentDate = getCurrentDate();
        System.out.println("当前日期:" + currentDate);

        // 获取当前时间
        Date currentTime = getCurrentTime();
        System.out.println("当前时间:" + currentTime);

        System.out.println("-----------------------------");
        // 获取当年的第一天
        Date firstDayOfYear = getFirstDayOfYear();
        System.out.println("当年的第一天:" + firstDayOfYear);
        // 获取当年的最后一天
        Date lastDayOfYear = getLastDayOfYear();
        System.out.println("当年的最后一天:" + lastDayOfYear);
        // 获取当年的第一天 日期
        Date firstDayOfYearDate = getFirstDayOfYearAddDate();
        System.out.println("当年的第一天日期:" + firstDayOfYearDate);
        // 获取当年的最后一天 日期
        Date lastDayOfYearDate = getLastDayOfYearAddDate();
        System.out.println("当年的最后一天日期:" + lastDayOfYearDate);
        // 获取当年的第一天 字符串
        String firstDayOfYearString = getFirstDayOfYearAddString();
        System.out.println("当年的第一天字符串:" + firstDayOfYearString);
        // 获取当年的最后一天 字符串
        String lastDayOfYearString = getLastDayOfYearAddString();
        System.out.println("当年的最后一天字符串:" + lastDayOfYearString);


        System.out.println("-----------------------------");


        // 获取当月的第一天
        Date firstDayOfMonth = getFirstDayOfMonth();
        System.out.println("当月的第一天:" + firstDayOfMonth);
        // 获取当月的最后一天
        Date lastDayOfMonth = getLastDayOfMonth();
        System.out.println("当月的最后一天:" + lastDayOfMonth);
        // 获取当月的第一天
        Date firstDayOfMonthAddDate = getFirstDayOfMonthAddDate();
        System.out.println("当月的第一天:" + firstDayOfMonthAddDate);
        // 获取当月的最后一天
        Date lastDayOfMonthAddDate = getLastDayOfMonthAddDate();
        System.out.println("当月的最后一天:" + lastDayOfMonthAddDate);
        // 获取当月的第一天
        String firstDayOfMonthAddString = getFirstDayOfMonthAddString();
        System.out.println("当月的第一天:" + firstDayOfMonthAddString);
        // 获取当月的最后一天
        String lastDayOfMonthAddString = getLastDayOfMonthAddString();
        System.out.println("当月的最后一天:" + lastDayOfMonthAddString);

        System.out.println("-----------------------------");
        // 获取当周的第一天
        Date firstDayOfWeek = getFirstDayOfWeek();
        System.out.println("当周的第一天:" + firstDayOfWeek);
        // 获取当周的最后一天
        Date lastDayOfWeek = getLastDayOfWeek();
        System.out.println("当周的最后一天:" + lastDayOfWeek);
        // 获取当周的第一天 日期
        Date firstDayOfWeekAddDate = getFirstDayOfWeekAddDate();
        System.out.println("当周的第一天日期:" + firstDayOfWeekAddDate);
        // 获取当周的最后一天 日期
        Date lastDayOfWeekAddDate = getLastDayOfWeekAddDate();
        System.out.println("当周的最后一天日期:" + lastDayOfWeekAddDate);
        // 获取当周的第一天
        String firstDayOfWeekAddString = getFirstDayOfWeekAddString();
        System.out.println("当周的第一天字符串:" + firstDayOfWeekAddString);
        // 获取当周的最后一天
        String lastDayOfWeekAddString = getLastDayOfWeekAddString();
        System.out.println("当周的最后一天字符串:" + lastDayOfWeekAddString);

        System.out.println("-----------------------------");


        // 获取当天时间加上00:00:00 的日期格式
        Date firstDayAddDate = getFirstDayAddDate();
        System.out.println("当天加上起始时间日期:" + firstDayAddDate);
        // 获取当天时间加上23:59:59 的日期格式
        Date lastDayAddDate = getLastDayAddDate();
        System.out.println("当天加上结束时间日期:" + lastDayAddDate);
        // 获取当天时间加上00:00:00 的字符串格式
        String firstDayAddString = getFirstDayAddString();
        System.out.println("当天加上起始时间字符串:" + firstDayAddString);
        // 获取当天时间加上23:59:59 的字符串格式
        String lastDayAddString = getLastDayAddString();
        System.out.println("当天加上结束字符串:" + lastDayAddString);
    }
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值