zoukankan      html  css  js  c++  java
  • Java关于日期的计算持续汇总~

     1     /**
     2      * 00
     3      * 描述:传入Date date.转为 String yyyyMMdd.
     4      * 【时间 2019-04-18 15:41:12 作者 陶攀峰】
     5      */
     6     public static String getDateToString(Date date) {
     7         Calendar calendar = Calendar.getInstance();
     8         calendar.setTime(date);
     9         return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
    10     }
    0描述:传入Date date.返回String yyyyMMdd.
     1 /**
     2      * 01 
     3      * 描述:获取传入日期前一天 .
     4      * 传入String yyyyMMdd.返回String yyyyMMdd.
     5      * 【时间 2019-04-15 16:20:44 作者 陶攀峰】
     6      */
     7     public static String getYesterday(String y_date) {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         try {
    11             calendar.setTime(simpleDateFormat.parse(y_date));
    12         } catch (ParseException e) {
    13             e.printStackTrace();
    14         }
    15         calendar.add(Calendar.DATE, - 1);
    16         return simpleDateFormat.format(calendar.getTime());
    17     }
    1描述:获取传入日期前一天 .
     1 /**
     2      * 02 
     3      * 描述:获取传入日期前七天 .
     4      * 传入String yyyyMMdd.返回String yyyyMMdd.
     5      * 【时间 2019-04-15 16:20:56 作者 陶攀峰】
     6      */
     7     public static String getBeforeSevenDay(String y_date) {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         try {
    11             calendar.setTime(simpleDateFormat.parse(y_date));
    12         } catch (ParseException e) {
    13             e.printStackTrace();
    14         }
    15         calendar.add(Calendar.DATE, - 7);
    16         return simpleDateFormat.format(calendar.getTime());
    17     }
    2描述:获取传入日期前七天 .
     1 /**
     2      * 03
     3      * 描述:获取传入日期前六天 .
     4      * 传入String yyyyMMdd.返回String yyyyMMdd.
     5      * 【时间 2019-04-15 16:21:07 作者 陶攀峰】
     6      */
     7     public static String getBeforeSixDay(String y_date) {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         try {
    11             calendar.setTime(simpleDateFormat.parse(y_date));
    12         } catch (ParseException e) {
    13             e.printStackTrace();
    14         }
    15         calendar.add(Calendar.DATE, - 6);
    16         return simpleDateFormat.format(calendar.getTime());
    17     }
    3描述:获取传入日期前六天 .
     1 /**
     2      * 04
     3      * 描述:获取传入日期所在月的第一天.
     4      * 传入String yyyyMMdd.返回String yyyyMMdd.
     5      * 【时间 2019-04-15 16:21:17 作者 陶攀峰】
     6      */
     7     public static String getNowMonthFirstDay(String y_date) {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         try {
    11             calendar.setTime(simpleDateFormat.parse(y_date));
    12         } catch (ParseException e) {
    13             e.printStackTrace();
    14         }
    15         calendar.set(Calendar.DAY_OF_MONTH, 1);
    16         return simpleDateFormat.format(calendar.getTime());
    17     }
    4描述:获取传入日期所在月的第一天.
     1     /**
     2      * 05
     3      * 描述:获取传入日期所在月的最后一天.
     4      * 传入String yyyyMMdd.返回String yyyyMMdd.
     5      * 【时间 2019-04-15 16:21:28 作者 陶攀峰】
     6      */
     7     public static String getNowMonthFinallyDay(String y_date) {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         try {
    11             calendar.setTime(simpleDateFormat.parse(y_date));
    12         } catch (ParseException e) {
    13             e.printStackTrace();
    14         }
    15         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    16         return simpleDateFormat.format(calendar.getTime());
    17     }    
    5描述:获取传入日期所在月的最后一天.
     1     /**
     2      * 06
     3      * 描述:获取传入日期上个月的第一天.
     4      * 传入String yyyyMMdd.返回String yyyyMMdd.
     5      * 【时间 2019-04-15 16:21:38 作者 陶攀峰】
     6      */
     7     public static String getLastMonthFirstDay(String y_date) {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         try {
    11             calendar.setTime(simpleDateFormat.parse(y_date));
    12         } catch (ParseException e) {
    13             e.printStackTrace();
    14         }
    15         calendar.add(Calendar.MONTH, -1);// 月份减一
    16         calendar.set(Calendar.DAY_OF_MONTH, 1);
    17         return simpleDateFormat.format(calendar.getTime());
    18     }
    6描述:获取传入日期上个月的第一天.
     1     /**
     2      * 07
     3      * 描述:获取传入日期上个月的最后一天.
     4      * 传入String yyyyMMdd.返回String yyyyMMdd.
     5      * 【时间 2019-04-15 16:21:47 作者 陶攀峰】
     6      */
     7     public static String getLastMonthFinallyDay(String y_date) {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         try {
    11             calendar.setTime(simpleDateFormat.parse(y_date));
    12         } catch (ParseException e) {
    13             e.printStackTrace();
    14         }
    15         calendar.add(Calendar.MONTH, -1);// 月份减一
    16         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    17         return simpleDateFormat.format(calendar.getTime());
    18     }
    7描述:获取传入日期上个月的最后一天.
     1 /**
     2      * 08
     3      * 描述:获取传入时间 + i 天 后的日期.
     4      * 参数格式String yyyyMMdd,int .返回String yyyyMMdd .
     5      * i=0表示今天.i=1表示明天.i=-1表示昨天.
     6      * 【时间 2019-04-15 16:21:57 作者 陶攀峰】
     7      */
     8     public static String getDayByI(String y_date, int i) {
     9         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    10         Calendar calendar = Calendar.getInstance();
    11         try {
    12             calendar.setTime(simpleDateFormat.parse(y_date));
    13         } catch (ParseException e) {
    14             e.printStackTrace();
    15         }
    16         calendar.add(Calendar.DATE, i);
    17         return simpleDateFormat.format(calendar.getTime());
    18     }
    8描述:获取传入时间 + i 天 后的日期.
     1 /**
     2      * 09
     3      * 描述:获取传入时间 + i 月 后的日期.
     4      * 参数格式String yyyyMMdd,int .返回String yyyyMMdd .
     5      * i=0 表示本月. i=1表示下个月 .i=-1表示上个月 
     6      * 【时间 2019-04-15 16:22:06 作者 陶攀峰】
     7      */
     8     public static String getMonthByI(String y_date,int i) {
     9         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    10         Calendar calendar = Calendar.getInstance();
    11         try {
    12             calendar.setTime(simpleDateFormat.parse(y_date));
    13         } catch (ParseException e) {
    14             e.printStackTrace();
    15         }
    16         calendar.add(Calendar.MONTH, i);// i为0 表示本月 i为1表示下个月 i为-1表示上个月 
    17         return simpleDateFormat.format(calendar.getTime()).substring(0, 6);
    18     }
    9描述:获取传入时间 + i 月 后的日期.
     1     /**
     2      * 10
     3      * 描述:获取当前系统时间.
     4      * 得到String yyyyMMdd .
     5      * 【时间 2019-04-15 16:22:15 作者 陶攀峰】
     6      */
     7     public static String getNowSystemDay() {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         calendar.setTime(new Date());
    11         return simpleDateFormat.format(calendar.getTime());
    12     }
    10描述:获取当前系统时间.
     1 /**
     2      * 11
     3      * 描述:获取当前系统时间的小时数(16:12 就返回16).
     4      * 返回int.
     5      * 【时间 2019年4月15日下午4:11:30 作者 陶攀峰】
     6      */
     7     public static int getNowSystemHour() {
     8         Calendar calendar = Calendar.getInstance();
     9         calendar.setTime(new Date());
    10         int hour = calendar.get(Calendar.HOUR_OF_DAY);
    11         return hour;
    12     }
    11描述:获取当前系统时间的小时数(16:12 就返回16).
     1 /**
     2      * 12
     3      * 描述:根据传进来的时间获取年份.
     4      * 参数格式String yyyyMMdd.返回int.
     5      * 【时间 2019-04-16 08:28:21 作者 陶攀峰】
     6      */
     7     public static int getYear(String y_date) {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         try {
    11             calendar.setTime(simpleDateFormat.parse(y_date));
    12         } catch (ParseException e) {
    13             e.printStackTrace();
    14         }
    15         int year = calendar.get(Calendar.YEAR);
    16         return year;
    17     }
    12描述:根据传进来的时间获取年份.
     1 /**
     2      * 13
     3      * 描述:根据传进来的时间获取月份.
     4      * 参数格式String yyyyMMdd.返回int.
     5      * 【时间 2019-04-16 08:30:45 作者 陶攀峰】
     6      */
     7     public static int getMonth(String y_date) {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         try {
    11             calendar.setTime(simpleDateFormat.parse(y_date));
    12         } catch (ParseException e) {
    13             e.printStackTrace();
    14         }
    15         int month = calendar.get(Calendar.MONTH) + 1;
    16         return month;
    17     }
    13描述:根据传进来的时间获取月份.
     1     /**
     2      * 14
     3      * 描述:根据传进来的时间获取天数
     4      * 参数格式String yyyyMMdd.返回int.
     5      * 【时间 2019-04-16 08:31:19 作者 陶攀峰】
     6      */
     7     public static int getDay(String y_date) {
     8         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
     9         Calendar calendar = Calendar.getInstance();
    10         try {
    11             calendar.setTime(simpleDateFormat.parse(y_date));
    12         } catch (ParseException e) {
    13             e.printStackTrace();
    14         }
    15         int day = calendar.get(Calendar.DATE);
    16         return day;
    17     }
    14描述:根据传进来的时间获取天数
     1 /**
     2      * 15
     3      * 描述:根据指定年和月 得到指定月的天数. 
     4      * 传入int year,int month. 返回 int. 
     5      * 【时间 2019-04-16 08:31:59 作者 陶攀峰】
     6      */
     7     public static int getDayByYearMonth(int year, int month) {
     8         Calendar a = Calendar.getInstance();
     9         a.set(Calendar.YEAR, year);
    10         a.set(Calendar.MONTH, month - 1);
    11         a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
    12         a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
    13         int maxDate = a.get(Calendar.DATE);
    14         return maxDate;
    15     }
    15描述:根据指定年和月 得到指定月的天数.
     1 /**
     2      * 16
     3      * 描述:把yyyyMMdd格式改为yyyy-MM-dd格式.
     4      * 参数格式String yyyyMMdd. 返回String yyyy-MM-dd. 
     5      * 【时间 2019-04-16 08:33:20 作者 陶攀峰】
     6      */
     7     public static String ChangeDateFormat(String y_date) {
     8         y_date = y_date.substring(0, 4) + "-" + y_date.substring(4, 6) + "-" + y_date.substring(6, 8);
     9         return y_date;
    10     }
    16描述:把yyyyMMdd格式改为yyyy-MM-dd格式.
     1 /**
     2      * 17
     3      * 描述:获取下个月一号的数据.
     4      * 传入String yyyyMMdd.返回String yyyyMMdd.
     5      * 例如:20181005 >>> 20181101 . 
     6      * 【时间 2019-04-16 08:34:43 作者 陶攀峰】
     7      */
     8     public static String getNextMonthFirstDay(String y_date) {
     9         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    10         Date date = null;
    11         try {
    12             date = sdf.parse(y_date);// 把设置的日期转换为日期格式
    13         } catch (ParseException e) {
    14             e.printStackTrace();
    15         }
    16         Calendar calendar = Calendar.getInstance();// 日历对象
    17         calendar.setTime(date);// 设置日期
    18         calendar.set(Calendar.DAY_OF_MONTH, 1);// 把当前日期天数设置为01
    19         calendar.add(Calendar.MONTH, 1);// 把月份减1
    20         return sdf.format(calendar.getTime());
    21     }
    17描述:获取下个月一号的数据.
     1 /**
     2      * 18
     3      * 描述:获取周一.
     4      * i=0 表示传入时间所在周的周一,i=1表示下周一,i=-1表示上周一.
     5      * 传入String yyyyMMdd,int. 返回 String yyyyMMdd. 
     6      * 【时间 2019-04-16 08:45:09 作者 陶攀峰】
     7      */
     8     public static String getMondayByIWeek(String y_date,int i) {
     9         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    10         Date date = null;
    11         try {
    12             date = sdf.parse(y_date);// 把设置的日期转换为日期格式
    13         } catch (ParseException e) {
    14             e.printStackTrace();
    15         }
    16         Calendar cal = Calendar.getInstance();
    17         cal.setTime(date);
    18 
    19         // 获得当前日期是一个星期的第几天
    20         int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
    21         if (1 == dayWeek) {
    22             cal.add(Calendar.DAY_OF_MONTH, -1);
    23         }
    24         // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
    25         cal.setFirstDayOfWeek(Calendar.MONDAY);
    26         // 获得当前日期是一个星期的第几天
    27         int day = cal.get(Calendar.DAY_OF_WEEK);
    28         // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
    29         cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
    30         cal.add(Calendar.DATE, 7 * i);
    31         return sdf.format(cal.getTime());
    32     }
    18描述:获取周一. i=0 表示传入时间所在周的周一,i=1表示下周一,i=-1表示上周一.
     1 /**
     2      * 19
     3      * 描述:返回日期所在星期几.星期一 为1、星期日为7.
     4      * 传入日期String yyyyMMdd. 返回int . 
     5      * 【时间 2019-04-16 08:48:30 作者 陶攀峰】
     6      */
     7     public static int getWeekNumber(String y_date) {
     8         Calendar cal = Calendar.getInstance();
     9         try {
    10             cal.setTime(new SimpleDateFormat("yyyyMMdd").parse(y_date));
    11         } catch (ParseException e) {
    12             e.printStackTrace();
    13         }
    14         return cal.get(Calendar.DAY_OF_WEEK)-1;
    15     }
    19描述:返回日期所在星期几.星期一 为1、星期日为7.
     1 /**
     2      * 20
     3      * 描述:周同期【开始>>以周一为开始】.
     4      * 【传入时间的上一年>>所在周的第一天>>(距离年周数相同>>一年第几周)】.
     5      * 例【2018-01-10>>2017-01-02】.
     6      * 【时间 2019年3月5日下午3:46:07 作者 陶攀峰】
     7      */
     8     public static String getWeekSamePhaseStart(String y_date) {
     9         SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMdd");
    10         Date date=null;
    11         try {
    12             date = sdf.parse(y_date);//把设置的日期转换为日期格式
    13         } catch (ParseException e) {
    14             e.printStackTrace();
    15         }
    16         Calendar calendar = Calendar.getInstance();//日历对象
    17         calendar.setTime(date);//设置日期
    18         int week_of_year=calendar.get(Calendar.WEEK_OF_YEAR);//当前日期是今年的第多少周【2】
    19         int year=calendar.get(Calendar.YEAR)-1;//得到今年年份、减去1得到去年年份【2018-1】     
    20         calendar.set(year, 0, 1);//设置日期为2017-01-01
    21         int dayofweek=7-calendar.get(Calendar.DAY_OF_WEEK)+1;//【7-1+1】
    22         week_of_year=week_of_year-2;//【2-2】
    23         calendar.add(Calendar.DAY_OF_YEAR, week_of_year*7+dayofweek);//【0*7+7】【此时日期由2017-01-01>>2017-01-08】
    24         int day_of_week = calendar.get(Calendar. DAY_OF_WEEK) - 1;//传入时间是本周的第几天【因为DAY_OF_WEEK以周日开始、所以减1】【1-1】
    25         if (day_of_week == 0 ) {//因为DAY_OF_WEEK以周日开始、所以原本的周日是第一天 改为第七天
    26             day_of_week = 7 ;
    27         }
    28         calendar.add(Calendar.DATE , -day_of_week + 1 );//【-7+1】【此时日期由2017-01-08>>2017-01-02】
    29         return sdf.format(calendar.getTime());
    30     }
    20描述:周同期【开始>>以周一为开始】.【传入时间的上一年>>所在周的第一天>>(距离年周数相同>>一年第几周)】.例【2018-01-10>>2017-01-02】.
     1     /**
     2      * 21
     3      * 描述:周同期【结束>>以周一为开始】.
     4      * 【上一年和传入时间所在同一个周的第一天>>(距离年周数相同>>一年第几周)(距离周天数相同>>一周第几天)】.
     5      * 例【2018-01-10>>2017-01-04】.
     6      * 【时间 2019年3月5日下午3:47:26 作者 陶攀峰】
     7      */
     8     public static String getWeekSamePhaseEnd(String y_date) {
     9         SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd");
    10         Date date=null;
    11         try {
    12             date = sdf.parse(y_date);//把设置的日期转换为日期格式
    13         } catch (ParseException e) {
    14             e.printStackTrace();
    15         }
    16         Calendar calendar = Calendar.getInstance();//日历对象
    17         calendar.setTime(date);//设置日期
    18         int day_of_week0 = calendar.get(Calendar. DAY_OF_WEEK) - 1;//传入时间是本周的第几天【因为DAY_OF_WEEK以周日开始、所以减1】【4-1】
    19         if (day_of_week0 == 0 ) {//因为DAY_OF_WEEK以周日开始、所以原本的周日是第一天 改为第七天
    20             day_of_week0 = 7 ;
    21         }
    22         int week_of_year=calendar.get(Calendar.WEEK_OF_YEAR);//当前日期是今年的第多少周【2】
    23         int year=calendar.get(Calendar.YEAR)-1;//得到今年年份、减去1得到去年年份【2018-1】     
    24         calendar.set(year, 0, 1);//设置日期为2017-01-01
    25         int dayofweek=7-calendar.get(Calendar.DAY_OF_WEEK)+1;//【7-1+1】
    26         week_of_year=week_of_year-2;//【2-2】
    27         calendar.add(Calendar.DAY_OF_YEAR, week_of_year*7+dayofweek);//【0*7+7】【此时日期由2017-01-01>>2017-01-08】
    28         int day_of_week = calendar.get(Calendar. DAY_OF_WEEK) - 1;//传入时间是本周的第几天【因为DAY_OF_WEEK以周日开始、所以减1】【1-1】
    29         if (day_of_week == 0 ) {//因为DAY_OF_WEEK以周日开始、所以原本的周日是第一天 改为第七天
    30             day_of_week = 7 ;
    31         }
    32         calendar.add(Calendar.DATE , -day_of_week + 1 );//【-7+1】【此时日期由2017-01-08>>2017-01-02】
    33         calendar.add(Calendar.DATE, day_of_week0-1);//【3-1】【此时日期由2017-01-02>>2017-01-04】
    34         return sdf.format(calendar.getTime());
    35     }
    21描述:周同期【结束>>以周一为开始】.【上一年和传入时间所在同一个周的第一天>>(距离年周数相同>>一年第几周)(距离周天数相同>>一周第几天)】.例【2018-01-10>>2017-01-04】.

     注意:

    以后有新的会更新,若不懂的也可以提问,错误的可以纠正,

    如果我这里面没有的 你们也可以提出来 我要会的话,也会写出来~~~

  • 相关阅读:
    php下 MVC实现的基本思路
    Apache 文件路径中“/”和“\”的问题
    PHP isset 函数作用
    适用于 php5.2 的 php.ini 中文版
    fedora17 用hostapd搭建无线wifi
    从看雪的一个沙箱代码中扣出的InlineHook代码
    添加psapi.h头文件之前要先添加Windows.h
    virtualbox中的window xp如何共享linux主机的文件
    Linux驱动开发之LDD3中第三章scull注释详解【转】
    Linux内核代码 结构体初始化【转】
  • 原文地址:https://www.cnblogs.com/taopanfeng/p/10729915.html
Copyright © 2011-2022 走看看