DateUtil(时间处理函数工具)

 

时间处理函数工具:得到时间戳,周一,周末,时间更改,时间精确计算。。。。

Java代码   收藏代码
  1. import java.text.ParseException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.Date;  
  5. import java.util.GregorianCalendar;  
  6. import java.util.TimeZone;  
  7.   
  8. /** 
  9.  * 时间处理函数 
  10.  *  
  11.  * @20080509 15:50 
  12.  */  
  13. public class DateUtil {  
  14.   
  15.     private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";  
  16.       
  17.     public static final String TIME_YEAR = "yyyy";  
  18.       
  19.     public static final String TIME_MONEN = "MM";  
  20.       
  21.     public static final String TIME_DAY = "dd";  
  22.   
  23.     public static String getDate(String interval, Date starttime, String pattern) {  
  24.         Calendar temp = Calendar.getInstance(TimeZone.getDefault());  
  25.         temp.setTime(starttime);  
  26.         temp.add(temp.MONTH, Integer.parseInt(interval));  
  27.         SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
  28.         return sdf.format(temp.getTime());  
  29.     }  
  30.   
  31.     /** 
  32.      * 将字符串类型转换为时间类型 
  33.      *  
  34.      * @return 
  35.      */  
  36.     public static Date str2Date(String str) {  
  37.         Date d = null;  
  38.         SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);  
  39.         try {  
  40.             d = sdf.parse(str);  
  41.         } catch (Exception e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.         return d;  
  45.     }  
  46.   
  47.     public static Date str2Date(String str, String pattern) {  
  48.         Date d = null;  
  49.         SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
  50.         try {  
  51.             d = sdf.parse(str);  
  52.         } catch (Exception e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.         return d;  
  56.     }  
  57.   
  58.     /** 
  59.      * 将时间格式化 
  60.      *  
  61.      * @return 
  62.      */  
  63.     public static Date DatePattern(Date date) {  
  64.         SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);  
  65.         try {  
  66.             String dd = sdf.format(date);  
  67.             date = str2Date(dd);  
  68.         } catch (Exception e) {  
  69.             e.printStackTrace();  
  70.         }  
  71.         return date;  
  72.     }  
  73.   
  74.     /** 
  75.      * 将时间格式化 
  76.      */  
  77.     public static Date DatePattern(Date date, String pattern) {  
  78.         SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
  79.         try {  
  80.             String dd = sdf.format(date);  
  81.             date = str2Date(dd, pattern);  
  82.         } catch (Exception e) {  
  83.             e.printStackTrace();  
  84.         }  
  85.         return date;  
  86.     }  
  87.   
  88.     public static String date2Str(Date date) {  
  89.         SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);  
  90.         return sdf.format(date);  
  91.     }  
  92.   
  93.     public static String date2Str(Date date, String format) {  
  94.         SimpleDateFormat sdf = new SimpleDateFormat(format);  
  95.         return sdf.format(date);  
  96.     }  
  97.   
  98.     /** 
  99.      * 获取昨天 
  100.      *  
  101.      * @param date 
  102.      * @return 
  103.      * @throws Exception 
  104.      */  
  105.     public static Date getLastDate(Date date) {  
  106.         Calendar calendar = Calendar.getInstance(TimeZone.getDefault());  
  107.         calendar.setTime(date);  
  108.   
  109.         calendar.add(calendar.DATE, -1);  
  110.   
  111.         return str2Date(date2Str(calendar.getTime()));  
  112.     }  
  113.     /** 
  114.      * 获取前几天 
  115.      * @param date 
  116.      * @return 
  117.      */  
  118.     public static Date getBeforeDate(Date date,int dates) {  
  119.         Calendar calendar = Calendar.getInstance(TimeZone.getDefault());  
  120.         calendar.setTime(date);  
  121.   
  122.         calendar.add(calendar.DATE, -dates);  
  123.   
  124.         return str2Date(date2Str(calendar.getTime()));  
  125.     }  
  126.   
  127.     /** 
  128.      * 获取上周第一天(周一) 
  129.      *  
  130.      * @param date 
  131.      * @return 
  132.      * @throws Exception 
  133.      */  
  134.     public static Date getLastWeekStart(Date date) {  
  135.         Calendar calendar = Calendar.getInstance(TimeZone.getDefault());  
  136.         calendar.setTime(date);  
  137.         int i = calendar.get(calendar.DAY_OF_WEEK) - 1;  
  138.         int startnum = 0;  
  139.         if (i == 0) {  
  140.             startnum = 7 + 6;  
  141.         } else {  
  142.             startnum = 7 + i - 1;  
  143.         }  
  144.         calendar.add(calendar.DATE, -startnum);  
  145.   
  146.         return str2Date(date2Str(calendar.getTime()));  
  147.     }  
  148.   
  149.     /** 
  150.      * 获取上周最后一天(周末) 
  151.      *  
  152.      * @param date 
  153.      * @return 
  154.      * @throws Exception 
  155.      */  
  156.     public static Date getLastWeekEnd(Date date) {  
  157.         Calendar calendar = Calendar.getInstance(TimeZone.getDefault());  
  158.         calendar.setTime(date);  
  159.         int i = calendar.get(calendar.DAY_OF_WEEK) - 1;  
  160.         int endnum = 0;  
  161.         if (i == 0) {  
  162.             endnum = 7;  
  163.         } else {  
  164.             endnum = i;  
  165.         }  
  166.         calendar.add(calendar.DATE, -(endnum - 1));  
  167.   
  168.         return str2Date(date2Str(calendar.getTime()));  
  169.     }  
  170.       
  171.     /** 
  172.      * 根据年和月得到天数 
  173.      * @param num 月 
  174.      * @param year 年 
  175.      * @return 
  176.      */  
  177.     public static int getday(int num,int year){  
  178.         if(num==1 || num==3 || num==5 || num==7 || num==8 || num==10 || num==12){  
  179.             return 31;  
  180.         }else if(num==2){  
  181.             //判断是否为闰年  
  182.             if(year%400==0 || (year%4==0 && year%100!=0)){  
  183.                 return 29;  
  184.             }else{  
  185.                 return 28;  
  186.             }  
  187.               
  188.         }else{  
  189.             return 30;  
  190.         }  
  191.     }  
  192.     /* 
  193.      * 计算当前日期距离下个月还有多少天 
  194.      */  
  195.     public static int getdaymis(Date time){  
  196.         int year = Integer.parseInt(  
  197.                 new SimpleDateFormat(TIME_YEAR).format(time));//年  
  198.           
  199.         int mm = Integer.parseInt(  
  200.                 new SimpleDateFormat(TIME_MONEN).format(time));//月  
  201.           
  202.         int dd = Integer.parseInt(  
  203.                 new SimpleDateFormat(TIME_DAY).format(time));//日  
  204.           
  205.           
  206.         //获取当前年月的总天数  
  207.         int sdd = getday(mm,year);  
  208.           
  209.         return sdd-dd;  
  210.           
  211.           
  212.     }  
  213.     /** 
  214.      * 日期转秒数 
  215.      * @param dateString 
  216.      * @return 
  217.      */  
  218.     public static long getTime(String dateString) {  
  219.         long time = 0;  
  220.         try {  
  221.             Date ret = null;  
  222.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  223.             ret = sdf.parse(dateString);  
  224.             time = ret.getTime()/1000;  
  225.         } catch (Exception e) {  
  226.           
  227.         }  
  228.         return time;  
  229.     }  
  230.       
  231.       
  232.     /** 
  233.      * 精确计算时间差,精确到日 
  234.      * @param fistill 起始日期 
  235.      * @param nowtime 结束日期 
  236.      * @param type type为1返回年月日(如:2年3个月零5天) 否则返回总的天数 
  237.      * @return 
  238.      */  
  239.     public static String patienage(Date fistill,Date nowtime,Integer type){  
  240.           
  241.         int fyear = Integer.parseInt(  
  242.                 new SimpleDateFormat(TIME_YEAR).format(fistill));//起始年  
  243.           
  244.         int fmm = Integer.parseInt(  
  245.                 new SimpleDateFormat(TIME_MONEN).format(fistill));//起始月  
  246.           
  247.         int fdd = Integer.parseInt(  
  248.                 new SimpleDateFormat(TIME_DAY).format(fistill));//起始日  
  249.           
  250.           
  251.         int nyear = Integer.parseInt(  
  252.                 new SimpleDateFormat(TIME_YEAR).format(nowtime));//结束年  
  253.           
  254.         int nmm = Integer.parseInt(  
  255.                 new SimpleDateFormat(TIME_MONEN).format(nowtime));//结束月  
  256.           
  257.         int ndd = Integer.parseInt(  
  258.                 new SimpleDateFormat(TIME_DAY).format(nowtime));//结束日  
  259.           
  260.         int cyear = nyear - fyear;  
  261.         int cmm = nmm - fmm;  
  262.         int cdd = ndd - fdd;  
  263.           
  264.           
  265.         int zyear = 0;  
  266.         int zmm = 0;  
  267.         int zdd = 0;  
  268.           
  269.         int countddd = 0;  //年月日累计天数  
  270.           
  271.         if(cdd<0){  
  272.             if(cmm<0){  
  273.                 zyear = cyear - 1;  
  274.                 zmm = (cmm + 12)-1;   
  275.                 int dd = getday(zmm,nyear-1);  
  276.                 zdd = dd + cdd;  
  277.                   
  278.                   
  279.                 countddd = zyear*365+zmm*30+zdd;  
  280.                   
  281.             }else if(cmm==0){  
  282.                 zyear = cyear - 1;  
  283.                 zmm = 12-1;   
  284.                 int dd = getday(zmm,nyear-1);  
  285.                 zdd = dd + cdd;  
  286.                   
  287.                 countddd = zyear*365+zmm*30+zdd;  
  288.                   
  289.             }else{  
  290.                 zyear = cyear;  
  291.                 zmm = cmm - 1;   
  292.                 int dd = getday(zmm,nyear);  
  293.                 zdd = dd + cdd;  
  294.                   
  295.                 countddd = zyear*365+zmm*30+zdd;  
  296.                   
  297.             }  
  298.         }else if(cdd==0){  
  299.             if(cmm<0){  
  300.                 zyear = cyear - 1;  
  301.                 zmm = cmm + 12;   
  302.                 zdd = 0;  
  303.                   
  304.                 countddd = zyear*365+zmm*30;  
  305.                   
  306.             }else if(cmm==0){  
  307.                 zyear = cyear;  
  308.                 zmm = 0;   
  309.                 zdd = 0;  
  310.                   
  311.                 countddd = zyear*365+zmm*30;  
  312.                   
  313.             }else{  
  314.                 zyear = cyear;  
  315.                 zmm = cmm;   
  316.                 zdd = 0;  
  317.                   
  318.                 countddd = zyear*365+zmm*30;  
  319.             }  
  320.         }else{  
  321.             if(cmm<0){  
  322.                 zyear = cyear - 1;  
  323.                 zmm = cmm + 12;   
  324.                 zdd = cdd;  
  325.                   
  326.                 countddd = zyear*365+zmm*30+zdd;  
  327.             }else if(cmm==0){  
  328.                 zyear = cyear;  
  329.                 zmm = 0;   
  330.                 zdd = cdd;  
  331.                   
  332.                 countddd = zyear*365+zmm*30+zdd;  
  333.             }else{  
  334.                 zyear = cyear;  
  335.                 zmm = cmm;   
  336.                 zdd = cdd;  
  337.                   
  338.                 countddd = zyear*365+zmm*30+zdd;  
  339.             }  
  340.         }  
  341.         String ptime = null;  
  342.           
  343.         if(zdd!=0){  
  344.             if(zmm!=0){  
  345.                 if(zyear!=0){  
  346.                     ptime = zyear+"年"+zmm+"个月"+"零"+zdd+"天";  
  347.                 }else{  
  348.                     ptime = zmm+"个月"+"零"+zdd+"天";  
  349.                 }  
  350.             }else{  
  351.                 if(zyear!=0){  
  352.                     ptime = zyear+"年"+"零"+zdd+"天";  
  353.                 }else{  
  354.                     ptime = zdd+"天";  
  355.                 }  
  356.             }  
  357.         }else{  
  358.             if(zmm!=0){  
  359.                 if(zyear!=0){  
  360.                     ptime = zyear+"年"+zmm+"个月";  
  361.                 }else{  
  362.                     ptime = zmm+"个月";  
  363.                 }  
  364.             }else{  
  365.                 if(zyear!=0){  
  366.                     ptime = zyear+"年";  
  367.                 }else{  
  368.                     ptime = null;  
  369.                 }  
  370.             }  
  371.         }  
  372.         if(type==1){  
  373.             return ptime;   //返回年月日(如:2年3个月零5天)  
  374.         }else{  
  375.             return String.valueOf(countddd);  //返回总天数  
  376.         }  
  377.           
  378.           
  379.     }  
  380.     /** 
  381.      * 得到月数 
  382.      * @param year 年数差 
  383.      * @param month 月数差 
  384.      * @return 
  385.      */  
  386.     public static int getCmm(Integer year,Integer month){  
  387.         int zmm = 0;  
  388.         if(month < 0){  
  389.             zmm = (month + 12)+(year-1)*12;  
  390.         }else if(month == 0){  
  391.             zmm = year*12;  
  392.         }else{  
  393.             zmm = year*12+month;  
  394.         }  
  395.         return zmm;  
  396.     }  
  397.       
  398.       
  399.   
  400.     /** 
  401.      * 改更现在时间 
  402.      */  
  403.     public static Date changeDate(String type, int value) {  
  404.         Calendar calendar = Calendar.getInstance(TimeZone.getDefault());  
  405.         if (type.equals("month")) {  
  406.             calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + value);  
  407.         } else if (type.equals("date")) {  
  408.             calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + value);  
  409.         }  
  410.         return calendar.getTime();  
  411.     }  
  412.   
  413.     /** 
  414.      * 更改时间 
  415.      */  
  416.     public static Date changeDate(Date date, String type, int value) {  
  417.         if (date != null) {  
  418.             // Calendar calendar = Calendar.getInstance(TimeZone.getDefault());  
  419.             Calendar calendar = new GregorianCalendar();  
  420.             calendar.setTime(date);  
  421.             // Calendar calendar = Calendar.  
  422.             if (type.equals("month")) {  
  423.                 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + value);  
  424.             } else if (type.equals("date")) {  
  425.                 calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + value);  
  426.             } else if (type.endsWith("year")) {  
  427.                 calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + value);  
  428.             }  
  429.             return calendar.getTime();  
  430.         }  
  431.         return null;  
  432.     }  
  433.   
  434.     /** 
  435.      * haoxw 比较时间是否在这两个时间点之间 
  436.      *  
  437.      * @param time1 
  438.      * @param time2 
  439.      * @return 
  440.      */  
  441.     public static boolean checkTime(String time1, String time2) {  
  442.         Calendar calendar = Calendar.getInstance();  
  443.         Date date1 = calendar.getTime();  
  444.         Date date11 = DateUtil.str2Date(DateUtil.date2Str(date1, "yyyy-MM-dd") + " " + time1);// 起始时间  
  445.   
  446.         Calendar c = Calendar.getInstance();  
  447.         c.add(Calendar.DATE, 1);  
  448.         Date date2 = c.getTime();  
  449.         Date date22 = DateUtil.str2Date(DateUtil.date2Str(date2, "yyyy-MM-dd") + " " + time2);// 终止时间  
  450.   
  451.         Calendar scalendar = Calendar.getInstance();  
  452.         scalendar.setTime(date11);// 起始时间  
  453.   
  454.         Calendar ecalendar = Calendar.getInstance();  
  455.         ecalendar.setTime(date22);// 终止时间  
  456.   
  457.         Calendar calendarnow = Calendar.getInstance();  
  458.   
  459.         if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {  
  460.             return true;  
  461.         } else {  
  462.             return false;  
  463.         }  
  464.   
  465.     }  
  466.       
  467.     /** 
  468.      * haoxw 比较时间是否在这两个时间点之间 
  469.      *  
  470.      * @param date11 
  471.      * @param date22 
  472.      * @return 
  473.      */  
  474.     public static boolean checkTime(Date date11, Date date22) {  
  475.           
  476.           
  477.   
  478.         Calendar scalendar = Calendar.getInstance();  
  479.         scalendar.setTime(date11);// 起始时间  
  480.   
  481.         Calendar ecalendar = Calendar.getInstance();  
  482.         ecalendar.setTime(date22);// 终止时间  
  483.   
  484.         Calendar calendarnow = Calendar.getInstance();  
  485.   
  486.         if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {  
  487.             return true;  
  488.         } else {  
  489.             return false;  
  490.         }  
  491.   
  492.     }  
  493.   
  494.       
  495.     public static boolean checkDate(String date1, String date2) {  
  496.   
  497.         Date date11 = DateUtil.str2Date(date1, "yyyy-MM-dd HH:mm:ss");// 起始时间  
  498.   
  499.         Date date22 = DateUtil.str2Date(date2, "yyyy-MM-dd HH:mm:ss");// 终止时间  
  500.   
  501.         Calendar scalendar = Calendar.getInstance();  
  502.         scalendar.setTime(date11);// 起始时间  
  503.   
  504.         Calendar ecalendar = Calendar.getInstance();  
  505.         ecalendar.setTime(date22);// 终止时间  
  506.   
  507.         Calendar calendarnow = Calendar.getInstance();  
  508.   
  509.         System.out.println(date11.toString());  
  510.         System.out.println(date22.toString());  
  511.         System.out.println(scalendar.toString());  
  512.         System.out.println(ecalendar.toString());  
  513.         System.out.println(calendarnow.toString());  
  514.   
  515.         if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {  
  516.             return true;  
  517.         } else {  
  518.             return false;  
  519.         }  
  520.     }  
  521.   
  522.     /** 
  523.      * 获取interval天之前的日期 
  524.      *  
  525.      * @param interval 
  526.      * @param starttime 
  527.      * @param pattern 
  528.      * @return 
  529.      */  
  530.     public static Date getIntervalDate(String interval, Date starttime, String pattern) {  
  531.         Calendar temp = Calendar.getInstance();  
  532.         temp.setTime(starttime);  
  533.         temp.add(temp.DATE, Integer.parseInt(interval));  
  534.         SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
  535.         String shijian = sdf.format(temp.getTime());  
  536.         return str2Date(shijian);  
  537.     }  
  538.       
  539.     public static Date formatDate(Date date){  
  540.         SimpleDateFormat bartDateFormat =   
  541.         new SimpleDateFormat("yyyy-MM-dd");           
  542.         System.out.println(bartDateFormat.format(date));   
  543.         SimpleDateFormat bartDateFormat1 =new SimpleDateFormat("yyyy-MM-dd");             
  544.         try {  
  545.             Date date1 = bartDateFormat1.parse(bartDateFormat.format(date));  
  546.         } catch (ParseException e) {                  
  547.             e.printStackTrace();  
  548.         }   
  549.         System.out.println(date.getTime());  
  550.         return date;   
  551.   
  552.     }  
  553.       
  554.     public static void main(String arf[]) {  
  555.   
  556.         /*String time1 = "2009-05-07 19:20:00"; 
  557.         String time2 = "2009-05-08 19:30:00"; 
  558.  
  559.         DateUtil d = new DateUtil(); 
  560.         System.out.println(d.checkDate(time1, time2)); 
  561.         System.out.println(d.date2Str(new Date()));*/  
  562.   
  563.         //System.out.println(d.getIntervalDate("-3", new Date(), DEFAULT_PATTERN));  
  564.         Calendar calendar = Calendar.getInstance(TimeZone.getDefault());  
  565.         System.out.println(calendar.toString());  
  566.         System.out.println(DateUtil.str2Date("20090731","yyyyMMdd"));  
  567.           
  568.         System.out.println(DateUtil.getBeforeDate(new Date(),2 ));  
  569.         System.out.println(DateUtil.DatePattern(new Date()));  
  570.           
  571.         SimpleDateFormat bartDateFormat =   
  572.         new SimpleDateFormat("yyyy-MM-dd");   
  573.         Date date = new Date();   
  574.         System.out.println("date;"+bartDateFormat.format(date));   
  575.         SimpleDateFormat bartDateFormat1 =new SimpleDateFormat("yyyy-MM-dd");             
  576.         try {  
  577.             Date date1 = bartDateFormat1.parse(bartDateFormat.format(date));  
  578.             System.out.println("日期:"+date1);   
  579.         } catch (ParseException e) {                  
  580.             e.printStackTrace();  
  581.         }   
  582.           
  583.     }  
  584.   
  585. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值