java最全最常用的日期工具类(随时补充)

简介: java最全最常用的日期工具类(随时补充)

@Slf4j
public final class DateUtils {

    private DateUtils() {
    }

    private static final String FORMAT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";

    public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    public static final String FORMAT_YYMMDD = "yyyyMMdd";

    /**
     * 把日期格式化为yyyy-MM-dd HH:mm:ss字符串
     *
     * @param date
     * @return
     */
    public static String formatTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_YYYYMMDDHHMMSS);
        return sdf.format(date);
    }

    /**
     * 把日期格式化为pattern字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String formatTime(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 把日期字符串转换为yyyy-MM-dd HH:mm:ss或者yyyy-MM-dd格式的日期
     *
     * @param date
     * @return
     */
    public static Date tDate(String date) {
        if (date == null) return null;
        SimpleDateFormat sdf = null;
        if (date.contains(GlobalManager.Symbol.MH)) {
            sdf = new SimpleDateFormat(FORMAT_YYYYMMDDHHMMSS);
        } else {
            sdf = new SimpleDateFormat(FORMAT_YYYY_MM_DD);
        }
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
        }
        return null;
    }

    /**
     * 把日期字符串转换为pattern格式的日期
     *
     * @param date
     * @param pattern
     * @return
     */
    public static Date tDate(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
        }
        return null;
    }

    /**
     * 日期加减法
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addDate(Date date, int day) {
        return new DateTime(date).plusDays(day).toDate();
    }

    /**
     * 日期加减法
     *
     * @param day
     * @return
     */
    public static Date addDate(int day) {
        return new DateTime(new Date()).plusDays(day).toDate();
    }

    /**
     * 获取格式为yyyy-MM-dd当天日期字符串
     *
     * @return
     */
    public static String getDate() {
        return formatTime(new Date(), FORMAT_YYYY_MM_DD);
    }

    /**
     * 获取格式为yyyy-MM-dd日期字符串
     *
     * @param date
     * @return
     */
    public static String getDate(Date date) {
        return formatTime(date, FORMAT_YYYY_MM_DD);
    }

    /**
     * 获取格式为yyyy-MM-dd HH:mm:ss当天日期字符串
     *
     * @return
     */
    public static String getTime() {
        return formatTime(new Date(), FORMAT_YYYYMMDDHHMMSS);
    }

    /**
     * 获取格式为yyyy-MM-dd HH:mm:ss日期字符串
     *
     * @param date
     * @return
     */
    public static String getTime(Date date) {
        return formatTime(date, FORMAT_YYYYMMDDHHMMSS);
    }

    /**
     * 获取年份
     *
     * @param date
     * @return
     */
    public static Integer getYear(Date date) {
        return Integer.valueOf(String.format("%tY", date));
    }

    /**
     * 获取月份
     *
     * @param date
     * @return
     */
    public static Integer getMonth(Date date) {
        return Integer.valueOf(String.format("%tm", date));
    }

    /**
     * 获取当前时间上个月的1号
     *
     * @return
     */
    public static String getLastMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        System.out.println("test当前时间:" + formatTime(calendar.getTime()));
//        calendar.add(Calendar.MONTH, -1);
//        calendar.set(Calendar.DAY_OF_MONTH, 1);

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        String firstDay = formatTime(calendar.getTime(), FORMAT_YYYY_MM_DD);
        return firstDay;
    }

    /**
     * 验证开始时间小于结束时间
     *
     * @param startDate 09:00
     * @param endDate   18:00
     * @return
     */
    public static Boolean timeCompare(String startDate, String endDate) {
        Boolean flag = Boolean.FALSE;
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            String dateStr = DateUtils.formatTime(new Date(), "yyyy-MM-dd");
            Date start = DateUtils.tDate(dateStr + " " + startDate, "yyyy-MM-dd HH:mm");
            Date end = DateUtils.tDate(dateStr + " " + endDate, "yyyy-MM-dd HH:mm");
            if (start != null && end != null) {
                long startDateLong = start.getTime();
                long endDateLong = end.getTime();
                if (startDateLong < endDateLong) {
                    flag = Boolean.TRUE;
                }
            }
        }
        return flag;
    }

    /**
     * 将时间转化为当天的日期
     *
     * @param time 09:00
     * @return
     */
    public static Date convertCurrentDateTime(String time) {
        Date date = new Date();
        if (StringUtils.isNotEmpty(time)) {
            String dateStr = DateUtils.formatTime(date, "yyyy-MM-dd");
            date = DateUtils.tDate(dateStr + " " + time, "yyyy-MM-dd HH:mm");
        }
        return date;
    }


    /**
     * 获取某天天开始时间
     */
    public static Date getStartTime(String dateTime) {
        Date date = DateUtils.tDate(dateTime);
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取某天结束时间
     */
    public static Date getEndTime(String dateTime) {

        Date date = DateUtils.tDate(dateTime);
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR, 23);
        todayStart.set(Calendar.MINUTE, 59);
        todayStart.set(Calendar.SECOND, 59);
        todayStart.set(Calendar.MILLISECOND, 999);
        return todayStart.getTime();
    }

    /**
     * 获取时间文件名
     *
     * @return
     */
    public static String dateTimeFile(String fileExtension) {
        //创建 StringBuffer 通过构造函数添加 文件累路径
        StringBuffer wavName = new StringBuffer("");
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        wavName.append(formatter.format(currentTime));
        int num = (int) (Math.random() * (10000 - 1000) + 1000);
        wavName.append(num);
        wavName.append(".");
        wavName.append(fileExtension);
        return wavName.toString();
    }

    /**
     * *@desc:  判断今天是星期几  测试输入第二年的1月 2月
     * *@param:  [date]
     * *@return:  java.lang.String
     * *@create:  zhaozhenkun
     * *@createtime:  2021/11/2
     * *@version: version 0.1
     */
    public static Integer getWeekDay(String time) {
        //必须yyyy-MM-dd
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        return localDate.getDayOfWeek().getValue();
    }

    /**
     * 获取明天的日期字符串
     *
     * @return
     */
    public static String tomorrowDateStr() {
        Date date = new Date();//取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //把日期往后增加一天.整数往后推,负数往前移动(1:表示明天、-1:表示昨天,0:表示今天)
        calendar.add(Calendar.DATE, 1);

        //这个时间就是日期往后推一天的结果
        date = calendar.getTime();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String tomorrowStr = formatter.format(date);
        return tomorrowStr;
    }

    /*
     * 获取日期的前一天
     * */
    public static String getSpecifiedDayBefore(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);

        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayBefore;
    }


    public static String addDateOne(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //把日期往后增加一天.整数往后推,负数往前移动(1:表示明天、-1:表示昨天,0:表示今天)
        calendar.add(Calendar.DATE, 1);

        //这个时间就是日期往后推一天的结果
        date = calendar.getTime();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String tomorrowStr = formatter.format(date);
        return tomorrowStr;
    }

    public static String dateToHM(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String str = sdf.format(date);
        return str;
    }

    /**
     * 加减对应时间后的日期
     *
     * @param date   需要加减时间的日期
     * @param amount 加减的时间(毫秒)
     * @return 加减对应时间后的日期
     */
    public static Date subtractTime(Date date, int amount) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String strTime = sdf.format(date.getTime() + amount);
            Date time = sdf.parse(strTime);
            return time;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getSevenDaysAgo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar c = Calendar.getInstance();

        c.add(Calendar.DATE, -7);

        Date monday = c.getTime();

        String preMonday = sdf.format(monday);

        String startTime = preMonday.substring(0, 10) + " 00:00:00";

        return startTime;
    }

    public static String getNowEndTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date today = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date day = calendar.getTime();
        String preMonday = sdf.format(day);
        return preMonday;
    }

    public static int dateComPareTo(String startDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int result = 0;
        try {
            Date date1 = sdf.parse(startDate);
            Date date2 = sdf.parse(endDate);
            if (date1.compareTo(date2) > 0) {
                result = 1;
            } else if (date1.compareTo(date2) < 0) {
                result = -1;
            } else if (date1.compareTo(date2) == 0) {
                result = 0;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getFourTeenDaysAgo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar c = Calendar.getInstance();

        c.add(Calendar.DATE, -14);

        Date monday = c.getTime();

        String preMonday = sdf.format(monday);

        String startTime = preMonday.substring(0, 10) + " 00:00:00";

        return startTime;
    }

    /**
     * 判断选择的日期本月的第几天
     */
    public static int getWeeksByChooseDay() {
        LocalDate now = LocalDate.now();
        int dayOfMonth = now.getDayOfMonth();
        return dayOfMonth;
    }

    public static Date getDaysNowSpliceTime(String time, Long minutes) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar c = Calendar.getInstance();

        Date monday = c.getTime();

        String preMonday = sdf.format(monday);

        String startTime = preMonday.substring(0, 10) + " " + time + ":00";

        Date date = tDate(startTime);

        Instant instant = date.toInstant();

        ZoneId zoneId = ZoneId.systemDefault();

        java.time.LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime().minusMinutes(minutes);

        ZonedDateTime zdt = localDateTime.atZone(zoneId);


        return Date.from(zdt.toInstant());
    }

    public static String subtractTimeToString(LocalDateTime dateTime, int num) {
        LocalDateTime localDateTime = dateTime.minusHours(num);
        return localDateTime.toString();
    }

    public static Boolean isAmOrPm(Date estimatedTimeStart) {
        try {
            if (estimatedTimeStart != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                Date currentTime = sdf.parse("12:00");
                String timeStart = DateUtils.dateToHM(estimatedTimeStart);
                Date start = sdf.parse(timeStart);
                return currentTime.after(start) ? true : false;
            } else {
                return true;
            }
        } catch (ParseException e) {
            return true;
        }
    }

    public static String getAmountDaysAgo(int amount) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar c = Calendar.getInstance();

        c.add(Calendar.DATE, amount);

        Date monday = c.getTime();

        String preMonday = sdf.format(monday);

        String startTime = preMonday.substring(0, 10);

        return startTime;
    }

    public static String getFirstDayOfLastMonthDay() {
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfThisMonth = today.withDayOfMonth(1);
        LocalDate firstDayOfLastMonth = firstDayOfThisMonth.minusMonths(1);
        return firstDayOfLastMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static String getLastDayOfLastMonthDay() {
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfThisMonth = today.withDayOfMonth(1);
        LocalDate lastDayOfLastMonth = firstDayOfThisMonth.minusDays(1);
        return lastDayOfLastMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static String getFirstDayOfThisMonth() {
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfThisMonth = today.withDayOfMonth(1);
        return firstDayOfThisMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static String getLastDayOfThisMonth() {
        LocalDate today = LocalDate.now();
        LocalDate lastDayOfThisMonth = today.withDayOfMonth(today.lengthOfMonth());
        return lastDayOfThisMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static String getFirstDayOfPreviousMonth() {
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfThisMonth = today.withDayOfMonth(1);
        LocalDate firstDayOfPreviousMonth = firstDayOfThisMonth.minusMonths(1);
        LocalDateTime firstDayOfPreviousMonthWithTime = firstDayOfPreviousMonth.atStartOfDay();
        return firstDayOfPreviousMonthWithTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String getLastDayOfPreviousMonth() {
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfThisMonth = today.withDayOfMonth(1);
        LocalDate lastDayOfPreviousMonth = firstDayOfThisMonth.minusDays(1);
        LocalDateTime lastSecondOfLastDayOfPreviousMonth = lastDayOfPreviousMonth.atTime(23, 59, 59);
        return lastSecondOfLastDayOfPreviousMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String getTodayStartTime() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime todayStart = now.withHour(0).withMinute(0).withSecond(0).withNano(0);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return todayStart.format(formatter);
    }
    //以获得所需格式的下个月的第一天
  public static String getNextMonthFirstDay() {
        LocalDate nextMonth = LocalDate.now().plusMonths(1);
        LocalDate firstDay = LocalDate.of(nextMonth.getYear(), nextMonth.getMonth(), 1);
        LocalDateTime firstDayDateTime = LocalDateTime.of(firstDay, LocalTime.MIN);
        return firstDayDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static void main(String[] args) {
        System.out.println(getTodayStartTime());
    }

}
目录
相关文章
|
1月前
|
算法 搜索推荐 Java
java 后端 使用 Graphics2D 制作海报,画echarts图,带工具类,各种细节:如头像切割成圆形,文字换行算法(完美实验success),解决画上文字、图片后不清晰问题
这篇文章介绍了如何使用Java后端技术,结合Graphics2D和Echarts等工具,生成包含个性化信息和图表的海报,并提供了详细的代码实现和GitHub项目链接。
104 0
java 后端 使用 Graphics2D 制作海报,画echarts图,带工具类,各种细节:如头像切割成圆形,文字换行算法(完美实验success),解决画上文字、图片后不清晰问题
|
1月前
|
Java API
Java的日期类都是怎么用的
【10月更文挑战第1天】本文介绍了 Java 中处理日期和时间的三个主要类:`java.util.Date`、`java.util.Calendar` 和 `java.time` 包下的新 API。`Date` 类用于表示精确到毫秒的瞬间,可通过时间戳创建或获取当前日期;`Calendar` 抽象类提供丰富的日期操作方法,如获取年月日及时区转换;`java.time` 包中的 `LocalDate`、`LocalTime`、`LocalDateTime` 和 `ZonedDateTime` 等类则提供了更为现代和灵活的日期时间处理方式,支持时区和复杂的时间计算。
38 14
|
1月前
|
Java
Java 些许公共工具类
Java 些许公共工具类
14 1
|
2月前
|
Java 数据库
java小工具util系列1:日期和字符串转换工具
java小工具util系列1:日期和字符串转换工具
51 3
|
2月前
|
安全 Java API
时间日期API(Date,SimpleDateFormat,Calendar)+java8新增日期API (LocalTime,LocalDate,LocalDateTime)
这篇文章介绍了Java中处理日期和时间的API,包括旧的日期API(Date、SimpleDateFormat、Calendar)和Java 8引入的新日期API(LocalTime、LocalDate、LocalDateTime)。文章详细解释了这些类/接口的方法和用途,并通过代码示例展示了如何使用它们。此外,还讨论了新旧API的区别,新API的不可变性和线程安全性,以及它们提供的操作日期时间的灵活性和简洁性。
|
3月前
|
前端开发 JavaScript Java
【前端学java】java中的日期操作(13)
【8月更文挑战第10天】java中的日期操作
23 2
【前端学java】java中的日期操作(13)
|
3月前
|
缓存 前端开发 Java
【前端学java】java基础巩固复习巩固语法练习-工具类的封装(14)
【8月更文挑战第10天】java基础巩固,工具类的封装
26 1
【前端学java】java基础巩固复习巩固语法练习-工具类的封装(14)
|
3月前
|
Java
Java应用结构规范问题之在UnitConvertUtils工具类将千米转换为米的问题如何解决
Java应用结构规范问题之在UnitConvertUtils工具类将千米转换为米的问题如何解决
|
3月前
|
Java
比较两个日期是否相等Java
这篇文章提供了Java中比较两个日期是否相等的两种方法:使用`Calendar`类和`SimpleDateFormat`类来确定两个日期是否为同一天,并附有详细的代码示例和测试结果。
|
3月前
|
存储 设计模式 安全
Java GenericObjectPool 对象池化技术--SpringBoot sftp 连接池工具类
Java GenericObjectPool 对象池化技术--SpringBoot sftp 连接池工具类
51 0