DateTimeUtils

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.hswebframework.utils;

import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public final class DateTimeUtils {
    public static final String YEAR = "yyyy";
    public static final String YEAR_MONTH = "yyyy-MM";
    public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
    public static final String YEAR_MONTH_DAY_SIMPLE = "yyyyMMdd";
    public static final String YEAR_MONTH_DAY_HOUR = "yyyy-MM-dd HH";
    public static final String YEAR_MONTH_DAY_HOUR_CN = "yyyy年MM月dd日HH时";
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE = "yyyy-MM-dd HH:mm";
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECOND = "yyyy-MM-dd HH:mm:ss";
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECOND_SIMPLE = "yyyyMMddHHmmss";
    public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";
    public static final String HOUR_MINUTE = "HH:mm";
    public static final String MONTH_DAY = "M.d";
    private static final int DAY_SECOND = 86400;
    private static final int HOUR_SECOND = 3600;
    private static final int MINUTE_SECOND = 60;
    public static final String REG_EXP_DATE = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";

    public DateTimeUtils() {
    }

    public static String format(Date date, String pattern) {
        return date == null ? "" : (new DateTime(date)).toString(pattern);
    }

    public static Date formatDateString(String dateString, String pattern) {
        try {
            DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(pattern);
            return dateTimeFormatter.parseDateTime(dateString).toDate();
        } catch (Exception var3) {
            return null;
        }
    }

    public static String getDayHourMinuteSecond(int second) {
        if (second == 0) {
            return "0秒";
        } else {
            StringBuilder sb = new StringBuilder();
            int days = second / 86400;
            if (days > 0) {
                sb.append(days);
                sb.append("天");
                second -= days * 86400;
            }

            int hours = second / 3600;
            if (hours > 0) {
                sb.append(hours);
                sb.append("小时");
                second -= hours * 3600;
            }

            int minutes = second / 60;
            if (minutes > 0) {
                sb.append(minutes);
                sb.append("分钟");
                second -= minutes * 60;
            }

            if (second > 0) {
                sb.append(second);
                sb.append("秒");
            }

            return sb.toString();
        }
    }

    public static String getDayHourMinute(int second) {
        if (second == 0) {
            return "0分钟";
        } else {
            StringBuilder sb = new StringBuilder();
            int days = second / 86400;
            if (days > 0) {
                sb.append(days);
                sb.append("天");
                second -= days * 86400;
            }

            int hours = second / 3600;
            if (hours > 0) {
                sb.append(hours);
                sb.append("小时");
                second -= hours * 3600;
            }

            int minutes = second / 60;
            if (minutes > 0) {
                sb.append(minutes);
                sb.append("分钟");
            }

            return sb.toString();
        }
    }

    public static DateTime getDateOnly(DateTime dateTime) {
        return new DateTime(dateTime.toString("yyyy-MM-dd"));
    }

    public static Date[] getMondayAndNextMonday() {
        DateTime dateTime = getDateOnly(new DateTime());
        DateTime monday = dateTime.dayOfWeek().withMinimumValue();
        DateTime nextMonday = monday.plusDays(7);
        return new Date[]{monday.toDate(), nextMonday.toDate()};
    }

    public static Date[] getMondayAndSunday(DateTime dateTime) {
        dateTime = getDateOnly(dateTime);
        DateTime monday = dateTime.dayOfWeek().withMinimumValue();
        DateTime sunday = monday.plusDays(6);
        return new Date[]{monday.toDate(), sunday.toDate()};
    }

    public static int compareDaysWithNow(Date date) {
        return Days.daysBetween(new DateTime(), new DateTime(date)).getDays();
    }

    public static int compareDaysWithToday(Date date) {
        DateTime today = new DateTime();
        today = new DateTime(today.getYear(), today.getMonthOfYear(), today.getDayOfMonth(), 0, 0, 0, 0);
        DateTime compareDay = new DateTime(date);
        compareDay = new DateTime(compareDay.getYear(), compareDay.getMonthOfYear(), compareDay.getDayOfMonth(), 0, 0, 0, 0);
        return Days.daysBetween(today, compareDay).getDays();
    }

    public static int compareDaysWithDay(Date a, Date b) {
        DateTime today = new DateTime(b);
        today = new DateTime(today.getYear(), today.getMonthOfYear(), today.getDayOfMonth(), 0, 0, 0, 0);
        DateTime compareDay = new DateTime(a);
        compareDay = new DateTime(compareDay.getYear(), compareDay.getMonthOfYear(), compareDay.getDayOfMonth(), 0, 0, 0, 0);
        return Days.daysBetween(today, compareDay).getDays();
    }

    public static boolean compareDateIgnoreMillisecond(Date date, Date compareDate) {
        if (date == null && compareDate == null) {
            return true;
        } else if (date == null && compareDate != null) {
            return false;
        } else if (date != null && compareDate == null) {
            return false;
        } else {
            return date.getTime() / 1000L == compareDate.getTime() / 1000L;
        }
    }

    public static int getDay(int second) {
        return second / 86400;
    }

    public static String getCompareWithTodayDateString(Date date) {
        int days = Math.abs(compareDaysWithToday(date));
        String dateString = "";
        if (days == 0) {
            dateString = "今天";
        } else if (days == 1) {
            dateString = "昨天";
        } else if (days == 2) {
            dateString = "2天前";
        } else if (days == 3) {
            dateString = "3天前";
        } else if (days == 4) {
            dateString = "4天前";
        } else if (days == 5) {
            dateString = "5天前";
        } else if (days == 6) {
            dateString = "6天前";
        } else if (days > 6 && days <= 14) {
            dateString = "1周前";
        } else if (days > 14 && days <= 21) {
            dateString = "2周前";
        } else if (days > 21 && days <= 30) {
            dateString = "3周前";
        } else if (days > 30) {
            dateString = "1月前";
        } else if (days > 365) {
            dateString = "1年前";
        } else if (days > 1095) {
            dateString = "3年前";
        }

        return dateString;
    }

    public static int compareMinutes(Date now, Date compareDate) {
        return (int)(now.getTime() - compareDate.getTime()) / '\uea60';
    }

    public static int getDayOfMonth(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.getDayOfMonth();
    }

    public static int getDateOfMonth(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfMonth().getMaximumValue();
    }

    public static int compareYear(Date date) {
        DateTime btd = new DateTime(date);
        DateTime nowDate = new DateTime();
        int year = 0;
        if (nowDate.getMonthOfYear() > btd.getMonthOfYear()) {
            year = nowDate.getYear() - btd.getYear();
        } else if (nowDate.getMonthOfYear() < btd.getMonthOfYear()) {
            year = nowDate.getYear() - btd.getYear() - 1;
        } else if (nowDate.getMonthOfYear() == btd.getMonthOfYear()) {
            if (nowDate.getDayOfMonth() >= btd.getDayOfMonth()) {
                year = nowDate.getYear() - btd.getYear();
            } else {
                year = nowDate.getYear() - btd.getYear() - 1;
            }
        }

        return year;
    }

    public static String compareDaysWithDate(Date date, Date date2) {
        StringBuilder msg = new StringBuilder();
        int minutes = (int)Math.abs((date.getTime() - date2.getTime()) / 60000L);
        if (minutes / 60 > 0 && minutes / 60 / 24 <= 0) {
            msg.append(minutes / 60 + "小时");
        }

        if (minutes / 60 / 24 > 0) {
            msg.append(minutes / 60 / 24 + "天");
            msg.append(minutes / 60 % 24 + "小时");
        }

        return msg.toString();
    }

    public static Date formatUnknownString2Date(String dateString) {
        try {
            if (StringUtils.isNullOrEmpty(dateString)) {
                return null;
            } else {
                dateString = dateString.replace("T", " ");
                String hms = "00:00:00";
                dateString = dateString.trim();
                if (dateString.contains(" ")) {
                    hms = dateString.substring(dateString.indexOf(" ") + 1, dateString.length());
                    dateString = dateString.substring(0, dateString.indexOf(" "));
                    hms = hms.replace(":", ":");
                    hms = hms.replace("时", ":");
                    hms = hms.replace("分", ":");
                    hms = hms.replace("秒", ":");
                    hms = hms.replace("-", ":");
                    hms = hms.replace("-", ":");
                    if (hms.endsWith(":")) {
                        hms = hms.substring(0, hms.length() - 1);
                    }

                    if (hms.split(":").length == 1) {
                        hms = hms + ":00:00";
                    }

                    if (hms.split(":").length == 2) {
                        hms = hms + ":00";
                    }
                }

                String[] hmsarr = hms.split(":");
                dateString = dateString.replace(".", "-");
                dateString = dateString.replace("/", "-");
                dateString = dateString.replace("-", "-");
                dateString = dateString.replace("年", "-");
                dateString = dateString.replace("月", "-");
                dateString = dateString.replace("日", "");
                String[] ymd = dateString.split("-");
                String yearStr = ymd[0];
                String monthStr = ymd.length > 1 ? ymd[1] : "";
                String dateStr = ymd.length > 2 ? ymd[2] : "";
                monthStr = monthStr == "" ? Integer.toString(1) : monthStr;
                dateStr = dateStr == "" ? Integer.toString(1) : dateStr;
                String dtr = yearStr + "-" + monthStr + "-" + dateStr + " " + hms;
                return !dtr.matches("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$") ? null : (new DateTime(Integer.parseInt(yearStr.trim()), Integer.parseInt(monthStr.trim()), Integer.parseInt(dateStr.trim()), Integer.parseInt(hmsarr[0].trim()), Integer.parseInt(hmsarr[1].trim()), Integer.parseInt(hmsarr[2].trim()), 0)).toDate();
            }
        } catch (Exception var8) {
            return null;
        }
    }

    public static Date[] formatDatesByString(String dateString, String spaceChar, String splitChar) {
        if (spaceChar.equals(splitChar)) {
            return null;
        } else {
            String[] dateStrs = dateString.split(splitChar);
            Date[] dates = new Date[dateStrs.length];
            int i = 0;

            for(int size = dateStrs.length; i < size; ++i) {
                dates[i] = formatUnknownString2Date(dateStrs[i]);
            }

            return dates;
        }
    }

    public static Date identityCard2Date(String identityCard) {
        try {
            String dateStr;
            if (identityCard.length() == 18) {
                dateStr = identityCard.substring(6, 14);
                return formatDateString(dateStr, "yyyyMMdd");
            } else if (identityCard.length() == 15) {
                dateStr = identityCard.substring(6, 12);
                return formatDateString(dateStr, "yyMMdd");
            } else {
                return null;
            }
        } catch (Exception var2) {
            return null;
        }
    }

    public static boolean validDate(String str) {
        try {
            Date date = formatUnknownString2Date(str);
            return date != null;
        } catch (Exception var2) {
            return false;
        }
    }
}

 

Java 8引入了新的日期时间API,称为“java.time”。它包括许多类和接口,用于日期时间操作。下面是一个基于Java 8的日期时间工具类: ```java import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class DateTimeUtils { private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss"); private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); /** * 获取当前日期 */ public static LocalDate getCurrentDate() { return LocalDate.now(); } /** * 获取当前时间 */ public static LocalTime getCurrentTime() { return LocalTime.now(); } /** * 获取当前日期时间 */ public static LocalDateTime getCurrentDateTime() { return LocalDateTime.now(); } /** * 格式化日期 */ public static String formatDate(LocalDate date) { return date.format(DATE_FORMATTER); } /** * 格式化时间 */ public static String formatTime(LocalTime time) { return time.format(TIME_FORMATTER); } /** * 格式化日期时间 */ public static String formatDateTime(LocalDateTime dateTime) { return dateTime.format(DATE_TIME_FORMATTER); } /** * 解析日期 */ public static LocalDate parseDate(String dateStr) { return LocalDate.parse(dateStr, DATE_FORMATTER); } /** * 解析时间 */ public static LocalTime parseTime(String timeStr) { return LocalTime.parse(timeStr, TIME_FORMATTER); } /** * 解析日期时间 */ public static LocalDateTime parseDateTime(String dateTimeStr) { return LocalDateTime.parse(dateTimeStr, DATE_TIME_FORMATTER); } } ``` 使用示例: ```java LocalDate currentDate = DateTimeUtils.getCurrentDate(); LocalTime currentTime = DateTimeUtils.getCurrentTime(); LocalDateTime currentDateTime = DateTimeUtils.getCurrentDateTime(); String dateStr = "2021-01-01"; LocalDate date = DateTimeUtils.parseDate(dateStr); String timeStr = "12:30:00"; LocalTime time = DateTimeUtils.parseTime(timeStr); String dateTimeStr = "2021-01-01 12:30:00"; LocalDateTime dateTime = DateTimeUtils.parseDateTime(dateTimeStr); String formattedDate = DateTimeUtils.formatDate(currentDate); String formattedTime = DateTimeUtils.formatTime(currentTime); String formattedDateTime = DateTimeUtils.formatDateTime(currentDateTime); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值