zoukankan      html  css  js  c++  java
  • 日期的简单封装

      1 import java.text.DateFormat;
      2 import java.text.ParseException;
      3 import java.text.SimpleDateFormat;
      4 import java.util.Calendar;
      5 import java.util.Date;
      6 
      7 import org.apache.commons.lang.StringUtils;
      8 
      9 public class DateUtil {
     10 
     11     public static long daysDiff(Date firstDay, Date lastDay) {
     12         if (firstDay == null || lastDay == null) {
     13             return 0;
     14         }
     15         long allDays = (lastDay.getTime() - (firstDay.getTime()))
     16                 / (1000 * 24 * 60 * 60);
     17 
     18         return allDays;
     19     }
     20 
     21     public static Long yearDiff(Date firstDay, Date lastDay) {
     22         if (firstDay == null || lastDay == null)
     23             return null;
     24         Calendar first = Calendar.getInstance();
     25         first.setTime(firstDay);
     26         Calendar last = Calendar.getInstance();
     27         last.setTime(lastDay);
     28         long years = last.get(Calendar.YEAR) - first.get(Calendar.YEAR);
     29         return years;
     30     }
     31 
     32     public static Date addMinute(Date src, int minute) {
     33         Calendar c = Calendar.getInstance();
     34         c.setTime(src);
     35         c.add(Calendar.MINUTE, minute);
     36         return c.getTime();
     37     }
     38 
     39     public static String getTodayTextYMD() {
     40         DateFormat df = new SimpleDateFormat("yyyyMMdd");
     41         return df.format(new Date());
     42 
     43     }
     44 
     45     public static String getYesterdayTextYMD() {
     46         Calendar c = Calendar.getInstance();
     47         c.add(Calendar.DAY_OF_YEAR, -1);
     48         DateFormat df = new SimpleDateFormat("yyyyMMdd");
     49         return df.format(c.getTime());
     50 
     51     }
     52 
     53     public static Date getYesterday() {
     54         Calendar c = Calendar.getInstance();
     55         c.add(Calendar.DAY_OF_YEAR, -1);
     56         return c.getTime();
     57     }
     58 
     59     public static Date parseDateYMD(String ymd) {
     60         DateFormat df = new SimpleDateFormat("yyyyMMdd");
     61         try {
     62             return df.parse(ymd);
     63         } catch (ParseException e) {
     64             return getYesterday();
     65         }
     66     }
     67     
     68     public static Date parseDate(String strDate,String dateFormat) {
     69         DateFormat df = new SimpleDateFormat(dateFormat);
     70         try {
     71             return df.parse(strDate);
     72         } catch (ParseException e) {
     73             return null;
     74         }
     75     }
     76     
     77     /**
     78      * 将传入的日期以指定格式转成字符串
     79      * 
     80      * @param format
     81      * @param dt
     82      * @return
     83      */
     84     public static String formatDate(String format, Date dt) {
     85         if (dt == null) {
     86             return "";
     87         }
     88         if(StringUtils.isBlank(format)){
     89             return "";
     90         }
     91         
     92         SimpleDateFormat fmt = new SimpleDateFormat(format);
     93         return fmt.format(dt);
     94     }
     95     
     96     public static String getToday(){
     97         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
     98         return df.format(new Date());
     99     }
    100     
    101     /**
    102      * 获取给定日期的最后一天
    103      * @param dateStr
    104      * @return
    105      * @throws ParseException
    106      */
    107     public static Date getLastDayOfMonth(Date date) throws ParseException{
    108         if(date!=null){
    109             Calendar calendar = Calendar.getInstance();
    110             calendar.setTime(date);
    111             calendar.add(Calendar.MONTH, 1);
    112             calendar.set(Calendar.DAY_OF_MONTH,1);
    113             calendar.add(Calendar.DAY_OF_MONTH,-1);
    114             return calendar.getTime();
    115         }else{
    116             throw new IllegalArgumentException("The parameter dateStr not be specified!");
    117         }
    118     }
    119     
    120     public static int getDayOfWeek(Calendar calendar) {
    121         int today;
    122         if (calendar != null) {
    123             today = calendar.get(7);
    124         } else {
    125             today = Calendar.getInstance().get(7);
    126         }
    127         if (today == 1) {
    128             return 7;
    129         }
    130         return today - 1;
    131     }
    132     
    133     public static int getDayOfWeek(Date date) {
    134         if (date == null) {
    135             date = new Date();
    136         }
    137 
    138         Calendar cal = Calendar.getInstance();
    139         cal.setTime(date);
    140         return getDayOfWeek(cal);
    141     }
    142 }
      1 import java.util.Calendar;
      2 import java.util.Date;
      3 
      4 public class DateUtils {
      5 
      6     /** 源日期格式 */
      7     private static final String SOURCE_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
      8     
      9     /** 目标日期格式 */
     10     private static final String TARGET_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
     11     
     12     private static final String TARGET_DATE_PATTERN_01 = "yyyyMMdd";
     13     
     14     private static final String TARGET_DATE_PATTERN_02 = "yyyy-MM-dd";
     15     
     16     /**
     17      * 字符日期转Date
     18      * yyyy-MM-dd'T'HH:mm:ss
     19      *        |  源日期格式
     20      * @param scheduleDate
     21      * @return
     22      */
     23     public static Date convertDate(String scheduleDate){
     24         return DateUtil.parseDate(scheduleDate, SOURCE_DATE_PATTERN);
     25     }
     26     
     27     /**
     28      * 字符日期格式转换
     29      *  yyyy-MM-dd'T'HH:mm:ss
     30      *        |  源日期格式
     31      *  yyyy-MM-dd HH:mm:ss
     32      *        |  目标日期格式
     33      * @param scheduleDate
     34      * @return
     35      */
     36     public static String convertDate2Str(String scheduleDate){
     37         return convertDate2Str(scheduleDate, SOURCE_DATE_PATTERN, TARGET_DATE_PATTERN);
     38     }
     39     
     40     /**
     41      * 字符日期格式转换
     42      *  yyyy-MM-dd'T'HH:mm:ss
     43      *        |  源日期格式
     44      *  yyyyMMdd
     45      *        |  目标日期格式
     46      * @param scheduleDate
     47      * @return
     48      */
     49     public static String convertDate2Str01(String scheduleDate){
     50         return convertDate2Str(scheduleDate, SOURCE_DATE_PATTERN, TARGET_DATE_PATTERN_01);
     51     }
     52     /**
     53      * 字符日期格式转换
     54      *  yyyy-MM-dd'T'HH:mm:ss
     55      *        |  源日期格式
     56      *  yyyy-MM-dd
     57      *        |  目标日期格式
     58      * @param scheduleDate
     59      * @return
     60      */
     61     public static String convertDate2Str02(String scheduleDate){
     62         return convertDate2Str(scheduleDate, SOURCE_DATE_PATTERN, TARGET_DATE_PATTERN_02);
     63     }
     64     
     65     /**
     66      * 字符日期格式转换
     67      *  yyyy-MM-dd'T'HH:mm:ss
     68      *        |  源日期格式
     69      * @param scheduleDate
     70      * @param targetPattern  |  目标日期格式
     71      * @return
     72      */
     73     public static String convertDate2Str(String scheduleDate, String targetPattern){
     74         return convertDate2Str(scheduleDate, SOURCE_DATE_PATTERN, targetPattern);
     75     }
     76     
     77     /**
     78      * 字符日期格式转换
     79      * @param scheduleDate
     80      * @param sourcePattern  |  源日期格式
     81      * @param targetPattern  |  目标日期格式
     82      * @return
     83      */
     84     public static String convertDate2Str(String scheduleDate, String sourcePattern, String targetPattern){
     85         Date date = DateUtil.parseDate(scheduleDate, sourcePattern);
     86         return DateUtil.formatDate(targetPattern, date);
     87     }
     88     public static Date getYesterday() {
     89         Calendar c = Calendar.getInstance();
     90         c.add(Calendar.DAY_OF_YEAR, -1);
     91         return c.getTime();
     92     }
     93     
     94     /**
     95      * 
     96      * <p>
     97      * Sunday = 1
     98      * </p>
     99      * <p>
    100      * Monday = 2
    101      * </p>
    102      * <p>
    103      * Tuesday = 3
    104      * </p>
    105      * <p>
    106      * Wednesday = 4
    107      * </p>
    108      * <p>
    109      * Thursday = 5
    110      * </p>
    111      * <p>
    112      * Friday = 6
    113      * </p>
    114      * <p>
    115      * Saturday = 7
    116      * </p>
    117      * 
    118      * @param date
    119      * @return
    120      */
    121     public static int dayForWeek(Date date) {
    122         Calendar c = Calendar.getInstance();
    123         c.setTime(date);
    124         int weekday = c.get(Calendar.DAY_OF_WEEK);
    125         return weekday;
    126     }
    127     
    128     public static boolean isSunday(Date date) {
    129         return (1 == dayForWeek(date));
    130     }
    131     
    132     public static boolean isSaturday(Date date) {
    133         return (7 == dayForWeek(date));
    134     }
    135 }
      1 import java.text.ParseException;
      2 import java.text.SimpleDateFormat;
      3 import java.util.Calendar;
      4 import java.util.Date;
      5 
      6 import org.apache.commons.lang.StringUtils;
      7 
      8 public class IDateUtil {
      9     private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
     10     private static SimpleDateFormat sdf0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     11     private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
     12     
     13     public static String parseDateString(String s){
     14         if(s==null||"".equals(s)){
     15             return null;
     16         }else{
     17             try {
     18                 return sdf0.format(sdf.parse(s));    
     19             } catch (ParseException e) {
     20 //                e.printStackTrace();
     21                 return s;
     22             }
     23         }
     24     }
     25     public static String parseString(Date date){
     26         if(date!=null){
     27             return sdf1.format(date);
     28         }
     29         return "";
     30     }
     31     
     32     public static Date parseDate(String dateStr){
     33         if(!StringUtils.isBlank(dateStr)){
     34             try {
     35                 return sdf1.parse(dateStr);
     36             } catch (ParseException e) {
     37                 return new Date();
     38             }
     39         }
     40         return new Date();
     41     }
     42     
     43     /**
     44      * 从身份证获取出生日期,id需要位15位或18位身份证,否则失败
     45      * @param id
     46      * @return
     47      * @throws ParseException 
     48      */
     49     public static Date getBirthday(String id) throws ParseException{
     50         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
     51         if(id.trim().length() == 18){
     52             String birthday = id.substring(6, 14);
     53             Date date = dateFormat.parse(birthday);
     54             return date;
     55         }
     56         if(id.trim().length() == 15){
     57             String birthday = id.substring(6,12);
     58             Date date = dateFormat.parse("19" + birthday);
     59             return date;
     60         }
     61         return null;
     62     }
     63     
     64     /**
     65      * String转Date,根据formatStr的格式来,异常:ParseException
     66      * @param dateStr
     67      * @param formatStr
     68      * @return
     69      * @throws ParseException
     70      */
     71     public static Date parseDate(String dateStr, String formatStr) throws ParseException{
     72         SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
     73         return dateFormat.parse(dateStr);
     74     }
     75     
     76     /**
     77      * Date转String,根据formatStr的格式来
     78      * @param date
     79      * @param formatStr
     80      * @return
     81      */
     82     public static String formatDate(Date date, String formatStr){
     83         SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
     84         return dateFormat.format(date);
     85     }
     86 
     87     /**
     88      * 在相关时间上增加一定数量的天数,输入Date,输出Date
     89      * @param date
     90      * @param num
     91      * @return
     92      */
     93     public static Date addDay(Date date, int num) {
     94         Calendar startDT = Calendar.getInstance();
     95         startDT.setTime(date);
     96         startDT.add(Calendar.DAY_OF_MONTH, num);
     97         return startDT.getTime();
     98     }
     99     
    100 }

    记录一下,方便查询

  • 相关阅读:
    结对项目--四则运算“软件”之升级版
    个人项目--多元四则运算
    《构建之法》1-5章后感
    git bash的安装与配置
    随笔
    numpy数组及处理:效率对比
    完整的中英文词频统计
    组合数据类型,英文词频统计
    9.13作业2(完整温度转换、数字游戏、解析身份证号...)
    Mad Libs游戏,华氏与摄氏转换
  • 原文地址:https://www.cnblogs.com/kongxianghao/p/8572509.html
Copyright © 2011-2022 走看看