zoukankan      html  css  js  c++  java
  • DateUtils日期工具类

    import java.time.LocalDate;
    import java.time.Period;
    import java.time.temporal.TemporalAdjusters;
    
    public class DateUtils {
        //日期转字符串
        public static String dateStr(LocalDate date){
            return date.toString();
        }
    
        /**
         * 字符串转时间
         * @param t  "2021-03-03"
         * @return
         */
        public static LocalDate parse(String t) {
            return LocalDate.parse(t);
        }
    
        /**
         * 时间计算
         * @param date  不传则默认今天
         * @param day
         * @return
         */
        public static LocalDate calcDate(LocalDate date, int day) {
            return date.plusDays(day);
        }
        public static LocalDate calcDate(int day) {
            return LocalDate.now().plusDays(day);
        }
    
        /**
         * 计算两个日期之间相隔几天
         * @param before 开始 "2021-03-03"
         * @param after 结束 "2021-03-10"
         * @return 7
         */
        public static Integer duration(LocalDate before, LocalDate after){
            return Period.between(before, after).getDays();
        }
    
        /**
         * 获取指定月最后一天
         * @param date 不传默认当月
         * @return
         */
        public static LocalDate getLastDayOfMonth(LocalDate date){
            return date.with(TemporalAdjusters.lastDayOfMonth());
        }
        public static LocalDate getLastDayOfMonth(){
            return LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        }
        /**
         * 获取指定月第一天
         * @param date 不传默认当月
         * @return
         */
        public static LocalDate getFirstDayOfMonth(LocalDate date){
            return date.with(TemporalAdjusters.firstDayOfMonth());
        }
        public static LocalDate getFirstDayOfMonth(){
            return LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        }
    
        public static void main(String[] args) {
            LocalDate now = LocalDate.now();
            System.out.println(now.toEpochDay());
    
            System.out.println(DateUtils.duration(DateUtils.parse("2021-03-13"),DateUtils.parse("2021-03-10")));
        }
    }
  • 相关阅读:
    工具推荐-css3渐变生成工具
    IE6bug-overflow不能隐藏的bug
    cs3属性操作js
    多级联动下拉菜单(原生js)
    js表单验证大全
    js-运动框架(时间版)
    LeetCode 677. 键值映射
    LeetCode 28. Implement strStr()
    计网学习笔记(5)
    计网学习笔记(4)
  • 原文地址:https://www.cnblogs.com/yewook/p/14475878.html
Copyright © 2011-2022 走看看