zoukankan      html  css  js  c++  java
  • Java 日期工具类(日期,月份加减等)--转

    1. package util;  
    2.   
    3. import java.text.ParseException;  
    4. import java.text.SimpleDateFormat;  
    5. import java.util.Calendar;  
    6. import java.util.Date;  
    7.   
    8. /*** 
    9.  * 日期工具类 
    10.  *  
    11.  * @author damao 
    12.  * 
    13.  */  
    14. public class DateAndTimeUtil {  
    15.     /*** 
    16.      * 日期月份减一个月 
    17.      *  
    18.      * @param datetime 
    19.      *            日期(2014-11) 
    20.      * @return 2014-10 
    21.      */  
    22.     public static String dateFormat(String datetime) {  
    23.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");  
    24.         Date date = null;  
    25.         try {  
    26.             date = sdf.parse(datetime);  
    27.         } catch (ParseException e) {  
    28.             e.printStackTrace();  
    29.         }  
    30.         Calendar cl = Calendar.getInstance();  
    31.         cl.setTime(date);  
    32.         cl.add(Calendar.MONTH, -1);  
    33.         date = cl.getTime();  
    34.         return sdf.format(date);  
    35.     }  
    36.   
    37.     public static String dateFormat(Date date) {  
    38.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");  
    39.         return sdf.format(date);  
    40.     }  
    41.   
    42.     /**** 
    43.      * 传入具体日期 ,返回具体日期减一个月。 
    44.      *  
    45.      * @param date 
    46.      *            日期(2014-04-20) 
    47.      * @return 2014-03-20 
    48.      * @throws ParseException 
    49.      */  
    50.     public static String subMonth(String date) throws ParseException {  
    51.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
    52.         Date dt = sdf.parse(date);  
    53.         Calendar rightNow = Calendar.getInstance();  
    54.         rightNow.setTime(dt);  
    55.   
    56.         rightNow.add(Calendar.MONTH, -1);  
    57.         Date dt1 = rightNow.getTime();  
    58.         String reStr = sdf.format(dt1);  
    59.   
    60.         return reStr;  
    61.     }  
    62.   
    63.     /**** 
    64.      * 获取月末最后一天 
    65.      *  
    66.      * @param sDate 
    67.      *            2014-11-24 
    68.      * @return 30 
    69.      */  
    70.     private static String getMonthMaxDay(String sDate) {  
    71.         SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");  
    72.         Calendar cal = Calendar.getInstance();  
    73.         Date date = null;  
    74.         try {  
    75.             date = sdf_full.parse(sDate + "-01");  
    76.         } catch (ParseException e) {  
    77.             e.printStackTrace();  
    78.         }  
    79.         cal.setTime(date);  
    80.         int last = cal.getActualMaximum(Calendar.DATE);  
    81.         return String.valueOf(last);  
    82.     }  
    83.   
    84.     // 判断是否是月末  
    85.     public static boolean isMonthEnd(Date date) {  
    86.         Calendar cal = Calendar.getInstance();  
    87.         cal.setTime(date);  
    88.         if (cal.get(Calendar.DATE) == cal  
    89.                 .getActualMaximum(Calendar.DAY_OF_MONTH))  
    90.             return true;  
    91.         else  
    92.             return false;  
    93.     }  
    94.   
    95.     /*** 
    96.      * 日期减一天、加一天 
    97.      *  
    98.      * @param option 
    99.      *            传入类型 pro:日期减一天,next:日期加一天 
    100.      * @param _date 
    101.      *            2014-11-24 
    102.      * @return 减一天:2014-11-23或(加一天:2014-11-25) 
    103.      */  
    104.     public static String checkOption(String option, String _date) {  
    105.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
    106.         Calendar cl = Calendar.getInstance();  
    107.         Date date = null;  
    108.   
    109.         try {  
    110.             date = (Date) sdf.parse(_date);  
    111.         } catch (ParseException e) {  
    112.             e.printStackTrace();  
    113.         }  
    114.         cl.setTime(date);  
    115.         if ("pre".equals(option)) {  
    116.             // 时间减一天  
    117.             cl.add(Calendar.DAY_OF_MONTH, -1);  
    118.   
    119.         } else if ("next".equals(option)) {  
    120.             // 时间加一天  
    121.             cl.add(Calendar.DAY_OF_YEAR, 1);  
    122.         } else {  
    123.             // do nothing  
    124.         }  
    125.         date = cl.getTime();  
    126.         return sdf.format(date);  
    127.     }  
    128.   
    129.     /*** 
    130.      * 判断日期是否为当前月, 是当前月返回当月最小日期和当月目前最大日期以及传入日期上月的最大日和最小日 
    131.      * 不是当前月返回传入月份的最大日和最小日以及传入日期上月的最大日和最小日 
    132.      *  
    133.      * @param date 
    134.      *            日期 例如:2014-11 
    135.      * @return String[] 开始日期,结束日期,上月开始日期,上月结束日期 
    136.      * @throws ParseException 
    137.      */  
    138.     public static String[] getNow_Pre_Date(String date) throws ParseException {  
    139.   
    140.         String[] str_date = new String[4];  
    141.         Date now = new Date();  
    142.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");  
    143.         SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");  
    144.         String stMonth = sdf.format(now);  
    145.         String stdate = "";// 开始日期  
    146.         String endate = "";// 结束日期  
    147.         String preDate_start = "";// 上月开始日期  
    148.         String preDate_end = "";// 上月结束日期  
    149.   
    150.         // 当前月  
    151.         if (date.equals(stMonth)) {  
    152.             stdate = stMonth + "-01"; // 2014-11-01  
    153.             endate = sdf_full.format(now);// 2014-11-24  
    154.             preDate_start = subMonth(stdate);// 2014-10-01  
    155.             preDate_end = subMonth(endate);// 2014-10-24  
    156.         } else {  
    157.             // 非当前月  
    158.             String monthMaxDay = getMonthMaxDay(date);  
    159.             stdate = date + "-01";// 2014-10-01  
    160.             endate = date + "-" + monthMaxDay;// 2014-10-31  
    161.             preDate_start = subMonth(stdate);// 2014-09-01  
    162.             preDate_end = subMonth(endate);// 2014-09-30  
    163.         }  
    164.         str_date[0] = stdate;  
    165.         str_date[1] = endate;  
    166.         str_date[2] = preDate_start;  
    167.         str_date[3] = preDate_end;  
    168.   
    169.         return str_date;  
    170.     }  
    171.   
    172.     public static void main(String[] args) throws ParseException {  
    173.         /* 
    174.          * String a =DateAndTimeUtil.dateFormat(new Date()); 
    175.          * System.out.println(a); String b = 
    176.          * DateAndTimeUtil.subMonth("2014-03-31"); System.out.println(b); 
    177.          * SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date 
    178.          * dt=sdf.parse("2014-03-31"); 
    179.          * System.out.println(DateAndTimeUtil.isMonthEnd(dt)); 
    180.          */  
    181.         String str = null;  
    182.         // str = DateAndTimeUtil.checkOption("next", "2014-11-30");  
    183.         // str = getMonthMaxDay("2014-11-24");  
    184.         // str = dateFormat("2014-11");  
    185.         str = getNow_Pre_Date("2014-10")[0];  
    186.         System.out.println(str);  
    187.     }  
    188. }  
  • 相关阅读:
    hdoj 2063 过山车
    hdoj 2112 HDU Today
    hdoj 1874 畅通工程续
    hdoj 2544 最短路
    sound of the genuine
    复习webpack的常用loader
    node-sass安装报错
    react-debug
    react-router 4v 路由嵌套问题
    React 中使用sass
  • 原文地址:https://www.cnblogs.com/zxg-6/p/7760300.html
Copyright © 2011-2022 走看看