zoukankan      html  css  js  c++  java
  • 特殊日期处理

      1 import java.text.ParseException;
      2 import java.text.SimpleDateFormat;
      3 import java.util.Date;
      4 
      5 import jodd.datetime.JDateTime;
      6 
      7 /**
      8  * 日期处理工具类
      9  * 
     10  * @author Liuhl
     11  *
     12  */
     13 public class DateUtils {
     14 
     15     public static final String DEFAULTPATTERN = "yyyy-MM-dd:HH:mm:ss";
     16     
     17     private static String[] parseDatePatterns = {
     18         "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
     19         "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
     20         "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM",
     21         "HH:mm"};
     22     
     23     /**
     24      * 字符串转为date
     25      * 
     26      * @param str
     27      * @return
     28      */
     29     public static Date parseDate(String str) {
     30         if (str == null){
     31             return null;
     32         }
     33         try {
     34             return org.apache.commons.lang3.time.DateUtils.parseDate(str, parseDatePatterns);
     35         } catch (ParseException e) {
     36             return null;
     37         }
     38     }
     39     
     40     /**
     41      * 日期转字符串
     42      * 
     43      * @return
     44      */
     45     public static String parseString(Date date, String pattern){
     46         if(date == null){
     47             return null;
     48         }
     49         SimpleDateFormat sdf = new SimpleDateFormat((DateUtils.isEmpty(pattern))?DateUtils.DEFAULTPATTERN:pattern);
     50         return sdf.format(date);
     51     }
     52     
     53     /**
     54      * JDateTime
     55      * 字符串时间,格式化为指定格式  "10:30" "hh:mm" 自动补全其他时间
     56      * @param time
     57      * @param partten
     58      * @return
     59      */
     60     public static JDateTime parseDate(String time, String partten){
     61         JDateTime before = new JDateTime();
     62         before.parse(time, partten);
     63         JDateTime after = new JDateTime();
     64         after.setHour(before.getHour());
     65         after.setMinute(before.getMinute());
     66         return after;
     67     }
     68     
     69     /**
     70      * 判断当前时间在afterDate以后,在beforeDate以前
     71      * 
     72      * @param beforeDate
     73      * @param afterDate
     74      * @return
     75      */
     76     public static boolean isBeforeToAfter(Date beforeDate, Date afterDate){
     77         beforeDate = parseDate(parseString(beforeDate, "yyyy-MM-dd HH:mm:ss"));
     78         afterDate = parseDate(parseString(afterDate, "yyyy-MM-dd HH:mm:ss"));
     79         Date currentTime = parseDate(parseString(new Date(), "yyyy-MM-dd HH:mm:ss"));
     80         if(currentTime.after(afterDate) && currentTime.before(beforeDate)){
     81             return true;
     82         }
     83         return false;
     84     }
     85     
     86     /**
     87      * 字符串空判断
     88      * 
     89      * @param str
     90      * @return
     91      */
     92     public static boolean isEmpty(String str){
     93         if(str== null||"".equals(str)){
     94             return true;
     95         }
     96         return false;
     97     }
     98     
     99     public static void main(String[] args) {
    100     /*Date d = parseDate("2017.7.13");
    101     JDateTime t = parseDate("10-30", "hh-mm");
    102     boolean flag = isBeforeToAfter(parseDate("2017-7-13 15:51:10"), parseDate("2017-7-13 15:00:00"));
    103     System.out.println(flag);*/
    104     }
    105 }
  • 相关阅读:
    hdu 1712(分组背包)
    hdu 3033(好题,分组背包)
    阶乘除法(很久之前的一道题,感觉挺好的,遂记录之)
    hdu 1559(最大子矩阵)
    hdu 1080(LCS变形)
    POJ 3458 Colour Sequence
    HUST 1599 Multiple
    HDU 3903 Trigonometric Function
    HUST 1605 Gene recombination
    UVA 11551 Experienced Endeavour
  • 原文地址:https://www.cnblogs.com/lhl-shubiao/p/7207023.html
Copyright © 2011-2022 走看看