日期工具类

主要是自己日常用到的日期处理类,在结合网上朋友们写的日期处理类,整理了一下日期工具类。共享出来,大家如果有能用到的,可以用啊。

package com.chd.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 *
 * 日期处理工具类
 * @category:工具类
 * @author chd
 * @version 1.0 ,Jun 1, 2010
 */
public class DateUtil {
 /**
  *
  * 求日期的下一天
  * @param    字符串格式的日期
  * @return    返回 日期下一天的日期
  * @exception 异常描述
  */
 @SuppressWarnings("unused")
 private static String nextDayAfterDate(String date){
    String[] current = date.split("-");
    //分隔字符串格式的日期
    int year = Integer.parseInt(current[0]);
    int month =Integer.parseInt(current[1]);
    int day = Integer.parseInt(current[2]);
    //如果日数少于该月的最大天数,天数加一天
    if(day < DateUtil.maxDay(year, month)){
   day++;
     }else{
      //如果月份少于12,则月份加一个月
      if(month < 12){
      
       month++;
      }else{
          //如果月份大于等于12,则月份置1,年份加1年
       month = 1;
       year++;
      }
      //如果日数超过该月的天数,天数置1
      day = 1 ;
     }
    //计算出处理后的日期
    date =year+"-"+DateUtil.formatData(month)+"-"+DateUtil.formatData(day);
   
    return date;
 } 
 
 /**
  *
  * 日期数据格式的处理
  * @param     整型 data
  * @return    返回String类型的新数据
  * @exception 异常描述
  */
 private static String formatData(int data){
  //如果数字小于10,数字前加0
  if(data < 10){   
   return "0"+data;
  }else{
   return data+"";
  }
  
 }
 /**
  *
  * 判断是否是闰年
  * @param     整型 year
  * @return    返回 boolean
  * @exception 异常描述
  */
 private  static boolean  isLeap(int year){
  //如果是闰年返回true,否则为false
        if(year%4==0 && year%100!=0 ||year%400==0){
              return true;
         }
        else{
              return false;
        }
     }
    /**
     *
     * 求一个月的最大一天
     * @param     年,月
     * @return    返回 整型天数
     * @exception 异常描述
     */
 private static int maxDay(int year,int month)
 {
  //初始化12个月份的天数
        int[] months={31,0,31,30,31,30,31,31,30,31,30,31};
        //如果不是2月则返回该月份的天数
        if(month!=2){         
           return months[month-1];
         }
         else{
              //如果是闰年返回为29天,否则28天    
           if(isLeap(year)){     
           return 29;
           }
           else{      
            return 28;
          }
      }     
   }
   /**
    *
    * 比较两个字符串的日期的大小
    * @param     两个日期
    * @return    返回 boolean
    * @exception 异常描述
    */
 @SuppressWarnings("unused")
 private static boolean compareToDate(String dateFrom,String dateTo){
        //如果开始日期小于等于结束日期返回为true,否则为false
     if(dateFrom.compareTo(dateTo) <= 0){         
        return true;
       }
       return false;
      }
  /**
   *
   * 由日历获取星期几
   * @param    年,月,日
   * @return    返回整型星期 1:星期日;2:星期一;3:星期二;4:星期三;5:星期四;6:星期五;7:星期六;
   * @exception 异常描述
   */
 @SuppressWarnings("unused")
 private static int getWeekValue(int year,int month,int day){
    //实例化Calendar
   Calendar now = Calendar.getInstance();
   //设置某年某月某日的参数
   now.set(Calendar.YEAR, year);
   now.set(Calendar.MONTH, month-1);
   now.set(Calendar.DATE, day);
   return now.get(Calendar.DAY_OF_WEEK);
  }
    public enum DateFormatType {  
         /** 
          * 格式为:yyyy-MM-dd HH:mm:ss 
          */ 
         DATE_FORMAT_STR("yyyy-MM-dd HH:mm:ss"),  
         /** 
          * 格式为:yyyyMMddHHmmss 
          */ 
         SIMPLE_DATE_TIME_FORMAT_STR("yyyyMMddHHmmss"),  
  
         /** 
          * 格式为:yyyy-MM-dd 
          */ 
         SIMPLE_DATE_FORMAT_STR("yyyy-MM-dd"),  
  
         /** 
          * 格式为:yyyy/MM/dd 
          */ 
         SIMPLE_DATE_FORMAT_VIRGULE_STR("yyyy/MM/dd"),  
  
         /** 
          * 格式为:HH:mm:ss 
          */ 
         HOUR_MINUTE_SECOND("HH:mm:ss"),  
  
         /** 
          * 格式为:HH:mm 
          */ 
         HOUR_MINUTE("HH:mm");  
  
         private final String value;  
  
         DateFormatType(String formatStr) {  
             this.value = formatStr;  
         }  
  
         public String getValue() {  
             return value;  
         }  
     }   
 
    /** 
     * 获取当前时间日期的字符串  
     */ 
    public static String getCurrentDateStr(DateFormatType dateFormatType) {  
        Date date = getCurrentDate();  
        return (String) OpearationDate(date, dateFormatType.getValue());  
    }  
 
    /** 
     * 时间、日期格式化成字符串 
     */ 
    public static String formatDate(Date date, DateFormatType dateFormatType) {  
        return (String) OpearationDate(date, dateFormatType.getValue());  
    }  
 
    /** 
     * 从字符串解析成时间、日期 
     */ 
    public static Date parseDate(String dateStr, DateFormatType dateFormatType) {  
        return (Date) OpearationDate(dateStr, dateFormatType.getValue());  
    }  
 
    /** 
     * 获取当前系统时间(原始格式) 
     */ 
    public static Date getCurrentDate() {  
        Date date = new Date(System.currentTimeMillis());  
        return date;  
    }  
 
    /** 
     * 获取当前日期的年、月、日、时、分、秒 
     */ 
    public static int getCurrentTime(TimeFormatType timeFormatType) {  
        return getTime(getCurrentDate(), timeFormatType);  
    }  
 
    /** 
     * 获取指定日期的年、月、日、时、分、秒 
     */ 
    public static int getTime(Date date, TimeFormatType timeFormatType) {  
        try {  
            Calendar c = Calendar.getInstance();  
            c.setTime(date);  
            int type = timeFormatType.getValue();  
            int i = c.get(type);  
            return type == 2 ? i + 1 : i;  
        } catch (Exception e) {  
            throw new RuntimeException("获取失败", e);  
        }  
    }  
 
    /** 
     * 获取指定日期的毫秒数 
     */ 
    public static long getMillis(Date date) {  
        java.util.Calendar c = java.util.Calendar.getInstance();  
        c.setTime(date);  
        return c.getTimeInMillis();  
    }  
 
    /** 
     * 日期相加、减操作 
     *  
     * 所返回结果单位为:天数 
     */ 
    public static int operationDate(Date date, Date diffDate, DateOperationType dateOperationType) {  
        long add = getMillis(date) + getMillis(diffDate);  
        long diff = getMillis(date) - getMillis(diffDate);  
        return (int) ((dateOperationType.getValue() ? add : diff) / (24 * 3600 * 1000));  
    }  
 
    /** 
     * 日期月份相加、减操作 
     */ 
    public static Date operationDateOfMonth(Date date, int month, DateOperationType dateOperationType) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        c.add(Calendar.MONTH, dateOperationType.getValue() ? month : month - (month * 2));  
        return c.getTime();  
    }  

    /** 
     * 日期天数相加、减操作 
     */ 
    public static Date operationDateOfDay(Date date, int day, DateOperationType dateOperationType) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        long millis = c.getTimeInMillis();  
        long millisOfday = day * 24 * 3600 * 1000;  
        long sumMillis = dateOperationType.getValue() ? (millis + millisOfday) : (millis - millisOfday);  
        c.setTimeInMillis(sumMillis);  
        return c.getTime();  
    }  
 
    private static Object OpearationDate(Object object, String formatStr) {  
        if (object == null || null == formatStr || "".equals(formatStr)) {  
            throw new RuntimeException("参数不能为空");  
        }  
        SimpleDateFormat format = new SimpleDateFormat(formatStr);  
        try {  
            if (object instanceof Date)  
                return format.format(object);  
            else 
                return format.parse(object.toString());  
        } catch (Exception e) {  
            throw new RuntimeException("操作失败", e);  
        }  
 
    }  
 
    public enum DateOperationType {  
 
        /** 
         * 加操作 
         */ 
        ADD(true),  
 
        /** 
         * 减操作 
         */ 
        DIFF(false);  
 
        private final boolean value;  
 
        DateOperationType(boolean operation) {  
            this.value = operation;  
        }  
 
        public boolean getValue() {  
            return value;  
        }  
    }  
 
    public enum TimeFormatType {  
 
        YEAR(1), MONTH(2), DAY(5), HOUR(11), MINUTE(12), SECOND(13);  
        private final int value;  
 
        TimeFormatType(int formatStr) {  
            this.value = formatStr;  
        }  
 
        public int getValue() {  
            return value;  
        }  
    }   
 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值