DateUtil.java 6.4 KB
package com.skua.modules.guest.util;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author sonin
 * @date 2021/8/18 11:34
 */
public class DateUtil {

    private static DateUtil dateUtil;

    public static DateUtil getInstance() {
        if (dateUtil == null) {
            synchronized (DateUtil.class) {
                if (dateUtil == null) {
                    dateUtil = new DateUtil();
                }
            }
        }
        return dateUtil;
    }

    /**
     * 转化日期格式
     * @param dateString  要转换的日期字符串
     * @param format  转换后的格式 默认 yyyy-MM-dd
     * @return
     */
    public String convertDateFormat(String  dateString ,String format){
        // 使用 SimpleDateFormat 来定义你想要的日期格式
        SimpleDateFormat formatter = null;

        String formattedDate = null;
        try {
            if(format != null && format.length() >0 ){
                formatter = new SimpleDateFormat(format);
            }else{
                formatter = new SimpleDateFormat("yyyy-MM-dd");
            }
            if(dateString != null && dateString.length() > 0){
                // 将日期字符串转换为 Date 对象
                Date date = formatter.parse(dateString);
                // 再将 Date 对象格式化为字符串
                formattedDate = formatter.format(date);
                // 输出结果
                System.out.println(formattedDate); // 输出: 2024-08-19
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return formattedDate;
    }

    public String convertDateFormat(Date  date ,String format){
        // 使用 SimpleDateFormat 来定义你想要的日期格式
        SimpleDateFormat formatter = null;

        String formattedDate = null;
        try {
            if(format != null && format.length() >0 ){
                formatter = new SimpleDateFormat(format);
            }else{
                formatter = new SimpleDateFormat("yyyy-MM-dd");
            }
            // 将日期字符串转换为 Date 对象
            // 再将 Date 对象格式化为字符串
            formattedDate = formatter.format(date);
            // 输出结果
            System.out.println(formattedDate); // 输出: 2024-08-19

        } catch (Exception e) {
            e.printStackTrace();
        }
        return formattedDate;
    }

    /**
     * 获取7天日期数据,并指定显示格式
     * @param dateFormat
     * @return
     */
    public List<String> get7Day(String dateFormat) {
        if(dateFormat == null ){
            dateFormat = "yyyy-MM-dd";
        }
        LocalDate today = LocalDate.now();
        List<String> dates = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);

        for (int i = 0; i < 7; i++) {
            LocalDate date = today.minusDays(i);
            dates.add(date.format(formatter));
        }
        return dates;
    }

    /**
     * 获取今天日期
     *
     * @return
     */
    public String getToday() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取本周每天的日期
     *
     * @return
     */
    public List<String> getWeek() {
        List<String> weekList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        // 今天是一周中的第几天
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (calendar.getFirstDayOfWeek() == Calendar.SUNDAY) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        // 计算一周开始的日期
        calendar.add(Calendar.DAY_OF_MONTH, -dayOfWeek);
        for (int i = 1; i <= 7; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            weekList.add(simpleDateFormat.format(calendar.getTime()));
        }
        return weekList;
    }

    public List<String> getMonth() {
        List<String> monthList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int year = calendar.get(Calendar.YEAR); //年份
        int month = calendar.get(Calendar.MONTH) + 1; //月份
        int day = calendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            StringBuilder stringBuilder = new StringBuilder().append(year).append("-");
            if (month < 10) {
                stringBuilder.append(0).append(month).append("-");
            } else {
                stringBuilder.append(month).append("-");
            }
            if (i < 10) {
                stringBuilder.append(0).append(i);
            } else {
                stringBuilder.append(i);
            }
            monthList.add(stringBuilder.toString());
        }
        return monthList;
    }

    public Integer getYear() {
        Calendar calendar = Calendar.getInstance();
        // e.g: 2021
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 根据年月获取对应的月份天数
     **/
    private int getDaysByYearMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DATE, 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.get(Calendar.DATE);
    }

    /**
     * String => java.util.Date
     *
     * @param str        表示日期的字符串
     * @param dateFormat 传入字符串的日期表示格式(如:"yyyy-MM-dd HH:mm:ss")
     * @return java.util.Date类型日期对象(如果转换失败则返回null)
     */
    public static Date strToDate(String str, String dateFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        Date date = null;
        try {
            date = simpleDateFormat.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * Date -> LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
}