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")));
        }
    }
  • 相关阅读:
    day5_python之hashlib模块
    day6_python之pickle、shelve序列化和反序列化
    day6_python之json序列化和反序列化
    day6_python之configparser_模块
    day1_python之字符串的常用操作
    day6_python序列化之 json & pickle & shelve 模块
    python如何自动发送邮件
    python selenium 获取对象输入的属性值
    python selenium 处理悬浮窗口(baidu tj_more)
    python selenium处理JS只读(12306)
  • 原文地址:https://www.cnblogs.com/yewook/p/14475878.html
Copyright © 2011-2022 走看看