zoukankan      html  css  js  c++  java
  • 我的时间工具类

    开始记笔记了,一个好的开始

    package com.hanfengyeqiao.utils;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    /**
     * 日期工具类
     */
    public class DateUtil {
    
        /**
         * 根据Date型的日期,取Calendar型的日期
         * @param date Date型的日期
         * @return Calendar型的日期
         */
        public static Calendar getCalendar(Date date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal;
        }
    
        /**
         * 日期计算,日加减
         * @param date  初始日期
         * @param amount 天数增量(负数为减)
         * @return 计算后的日期
         */
        public static Date addDays(Date date, int amount) {
        	Calendar cd =getCalendar(date);   
            cd.add(Calendar.DATE, amount);
            return cd.getTime();
        }
    
        /**
         * 判断指定年份日期的年份是否为闰年
         * @param date 日期
         * @return 闰年ture,非闰年false
         */
        public static boolean isLeapYear(Date date) {
            int year = getCalendar(date).get(Calendar.YEAR);
            return isLeapYear(year);
        }
    
        /**
         * 判断指定年份日期的年份是否为闰年
         * @param year 年份数字
         * @return 闰年ture,非闰年false
         */
        public static boolean isLeapYear(int year) {
            if ((year % 400) == 0) {
                return true;
            }
            else if ((year % 4) == 0) {
                if ((year % 100) == 0) {
                    return false;
                }
                else {
                    return true;
                }
            }
            else {
                return false;
            }
        }
    
        /**
         * 	获取指定日期前一个月的第一天
         */
        public static String getAgoMonthFirstDay(Date date){
        	Calendar calendar =getCalendar(date);
        	SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd");
        	calendar.add(Calendar.MONTH, -1);
        	calendar.set(Calendar.DAY_OF_MONTH,1);
            return sdf.format(calendar.getTime());
        }
        
        /**
         * 	获取指定日期前一个月的最后一天
         */
        public static String getAgoMonthLastDay(Date date){
        	Calendar calendar =getCalendar(date);
        	SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd");
        	calendar.set(Calendar.DAY_OF_MONTH, 0);
            return sdf.format(calendar.getTime());
        }
        
        public static void main(String[] args) {
    		System.out.println(getAgoMonthLastDay(new Date()));
    	}
    }
    

      

    不管什么时候都别忘了最初的梦想
  • 相关阅读:
    .Net常用的命名空间
    Jquery测试纠错笔记
    第一章 学习总结
    Java和C++引用的区别
    gin的墙内开发艺术
    golang几个环境变量的问题
    Leetcode240_搜索二维矩阵II
    Leetcode1358_包含所有三种字符的子字符串数目
    Leetcode1354_多次求和构造目标数组
    Leetcode1353_最多可以参加的会议数目
  • 原文地址:https://www.cnblogs.com/hanfengyeqiao/p/9471458.html
Copyright © 2011-2022 走看看