自己写的关于时间处理的一个类

package com.zrar.zjgp.cfm.ctrl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * 描述:此类为一自定义日期时间类,包括对于时间的各种操作,返回值均为String字符串
 *      1。获得当前日期  getLocalDate()
 *      2。获得当前时间  getLocalTime()
 *      3。获得当前星期X  getLocalWeek()
 *      4。获得当前日期时间 getLocalDateTime()
 *      5。获得当前日期星期几 getLocalDateWeek()
 *      6。获得当前日期时间星期X getLocalDateTimeWeek()
 *      7。在当前日期增加或者减少年,月,日 getDateAdd××() 其中××为Year,Month,Day
 *      8。在自己定义的时间上增加或者减少小时,分钟,秒 getTimeAdd×× 其中××为hour,minute,second
 *      9。设置日期,时间
 *      10。计算两指定时间的时间差 getTimeMinus()
 *      11。计算两指定日期的时间差 结果为天数 getDateMinus()
 *      12。将从数据库中得到的时间差转换为标准的 XX 天 XX 小时 XX 分 XX 秒 getDBTimeMinus()
 *      13。日期转换 在String Date 和Calendar 之间互转 convert();
 *      14。返回传入的日期之间的所包含天数 getDateMinusList();
 *
 * 注意事项: 1.日期格式 2006-06-06  以"-"分割。
 *          2.时间格式 20:23:23   以":"分割。
 *          3.JDK 1.4 以上。
 *
 * 实例:
 *
 * @author wyhai
 */

public class ZrarDateTime {
    private  int      x;      //日期属性 年
    private  int      y;                    //日期属性 月
    private  int      z;                    //日期属性 日
    private  Calendar localTime;            //当前日期
    public ZrarDateTime()
    {
        localTime = Calendar.getInstance();
    }
    /**
     * 功能:得到当前日期 格式为:xxxx-yy-zz (eg: 2006-06-26)<br>
     * @return String
     * @author Administrator
     */
    public String getLocalDate()
    {
        String strY = null;
        String strZ = null;
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1; 
        z = localTime.get(Calendar.DATE);
        strY = y >= 10 ? String.valueOf(y) : ("0" + y) ;
        strZ = z >= 10 ? String.valueOf(z) : ("0" + z) ;
        return  x + "-" + strY + "-" + strZ;
    }
    /**
     * 功能:得到当前时间 格式为:xx:yy:zz (eg: 4:26:59)<br>
     * @return String
     * @author Administrator
     */
    public String getLocalTime()
    {
        String strY = null;
        String strZ = null;
        x = localTime.get(Calendar.HOUR);
        y = localTime.get(Calendar.MINUTE); 
        z = localTime.get(Calendar.SECOND);
        strY = y >= 10 ? String.valueOf(y) : ("0" + y) ;
        strZ = z >= 10 ? String.valueOf(z) : ("0" + z) ;
        return  x + ":" + strY + ":" + strZ;
    }
    /**
     *
     * 功能:得到当前时间    星期x<br>
     *
     * @return dateWeek
     * @author Administrator
     */
    public String getLocalWeek()
    {
        String dateWeek = null;
        int nWeek = 0;
        nWeek = localTime.get(Calendar.DAY_OF_WEEK) - 1;
        switch (nWeek)
        {
            case 1: dateWeek = "星期一" ; break;
            case 2: dateWeek = "星期二" ; break;
            case 3: dateWeek = "星期三" ; break;
            case 4: dateWeek = "星期四" ; break;
            case 5: dateWeek = "星期五" ; break;
            case 6: dateWeek = "星期六" ; break;
            case 0: dateWeek = "星期日" ; break;
        }
        return dateWeek;
    }
    /**
     * 功能:得到当前日期时间 格式为:xxxx-yy-zz xx:yy:zz (eg: 2006-06-26 4:26:59 )<br>
     * @return String dateTime
     * @author Administrator
     */
    public String getLocalDateTime()
    {
        String dateTime = getLocalDate() + " " + getLocalTime();
        return dateTime;
    }
    /**
     *
     * 功能:得到当前日期  格式为:xxxx-yy-zz 星期X<br>
     *
     * @return dateWeek
     * @author Administrator
     */
    public String getLocalDateWeek()
    {
        String dateWeek = null ;
        String [] xx = getLocalDate().split("-") ;
        if(xx.length==3)
        {
            dateWeek = xx[0] + "年" + xx[1]+ "月" + xx[2] + "日 " + getLocalWeek() ;
        }else
        {
            dateWeek = "日期格式为YYYY-MM-DD 你输入的有误!" ;
        }
        return dateWeek;  
    }
    /**
     *
     * 功能:得到当前日期时间 格式为:xxxx-yy-zz 星期X xx:yy:zz(eg: 2006-06-26 星期X 4:26:59 )<br> <br>
     *
     * @return
     * @author Administrator
     */
    public String getLocalDateTimeWeek()
    {
        String dateTimeWeek = getLocalDateWeek() + " " +getLocalTime() ;
        return dateTimeWeek;
    }
    /**
     * 功能:得到当前日期的前面,或者后面几天<br>
     * @param days (正数表示后面几天,负数表示前面几天)
     * @return String
     * @author Administrator
     */
    public String getDateAddDay(int days)
    {
        return  addDateAll(Calendar.DATE,days);      
    }
    /**
     * 功能:得到当前日期的前面,或者后面几月<br>
     * @param months (正数表示后面几月,负数表示前面几月)
     * @return String
     * @author Administrator
     */
    public String getDateAddMonth(int months)
    {
        return  addDateAll(Calendar.MONTH,months);
    }
    /**
     * 功能:得到当前日期的前面,或者后面几年<br>
     * @param years (正数表示后面几年,负数表示前面几年)
     * @return String
     * @author Administrator
     */
    public String getDateAddYear(int years)
    {
        return  addDateAll(Calendar.YEAR,years);
    }
    /**
     * 功能:设置自定义日期<br>
     * @param date
     * @return
     * @author Administrator
     */
    private void setDate(String date)
    {
        String dates[] = date.split("-");
        x = Integer.parseInt(dates[0].trim());
        y = Integer.parseInt(dates[1].trim()) - 1;
        z = Integer.parseInt(dates[2].trim());
        localTime.set(x,y,z);      
    }
    /**
     * 功能:设置自定义时间<br>
     * @param time
     * @return
     * @author Administrator
     */
    private void setTime(String time)
    {
        String times[] = time.split(":");
        x = Integer.parseInt(times[0]);
        y = Integer.parseInt(times[1]);
        z = Integer.parseInt(times[2]);
        localTime.set(1999,9,9,x,y,z);      
    }
    /**
     * 功能:得到自定义指定日期前面,或者后面的几天<br>
     * @param date (自定义日期时间 eg: 2006-06-06)
     * @param days (正数表示后面几天,负数表示前面几天)
     * @return String
     * @author Administrator
     */
    public String getDateAddDay(String date , int days)
    {
        if(date.split("-").length == 3)
        {
            setDate(date);
            return  addDateAll(Calendar.DATE,days);       
        }else
        {
            return "日期格式为YYYY-MM-DD 你输入的有误!" ;
        }
    }
    /**
     * 功能:得到自定义日期前面,或者后面的几月<br>
     * @param date  (自定义日期时间 eg: 2006-06-06)
     * @param months (正数表示后面几月,负数表示前面几月)
     * @return
     * @author Administrator
     */
    public String getDateAddMonth(String date , int months)
    {
        if(date.split("-").length == 3)
        {
            setDate(date);
            return  addDateAll(Calendar.MONTH,months);
        }else
        {
            return "日期格式为YYYY-MM-DD 你输入的有误!" ;
        }
    }
    /**
     * 功能:得到自定义日期前面,或者后面的几年<br>
     * @param date  (自定义日期时间 eg: 2006-06-06)
     * @param years  (正数表示后面几年,负数表示前面几年)
     * @return String
     * @author Administrator
     */
    public String getDateAddYear(String date , int years)
    {
        if(date.split("-").length == 3)
        {
            setDate(date);
            return  addDateAll(Calendar.YEAR,years); 
        }else
        {
            return "日期格式为YYYY-MM-DD 你输入的有误!" ;
        }
    }
    /**
     * 功能:得到当前时间的前面,或者后面几秒<br>
     * @param seconds (正数表示后面几秒,负数表示前面几秒)
     * @return String
     * @author Administrator
     */
    public String getTimeAddSecond(int seconds)
    {
        return  addTimeAll(Calendar.SECOND,seconds);
    }
    /**
     * 功能:得到当前时间的前面,或者后面几分<br>
     * @param minutes (正数表示后面几分,负数表示前面几分)
     * @return String
     * @author Administrator
     */
    public String getTimeAddMinute(int minutes)
    {
        return  addTimeAll(Calendar.MINUTE,minutes);
    }
    /**
     * 功能:得到当前时间的前面,或者后面几小时<br>
     * @param hours (正数表示后面几小时,负数表示前面几小时)
     * @return String
     * @author Administrator
     */
    public String getTimeAddHour(int hours)
    {
        return  addTimeAll(Calendar.HOUR,hours);
    }
    /**
     * 功能:得到指定时间的前面,或者后面几秒<br>
     * @param time (指定时间)
     * @param seconds (正数表示后面几秒,负数表示前面几秒)
     * @return String
     * @author Administrator
     */
    public String getTimeAddSecond(String time ,int seconds)
    {      
        if(time.split(":").length == 3)
        {
            setTime(time);
            return  addTimeAll(Calendar.SECOND,seconds);
        }else
        {
            return "时间格式为hh:mm:ss 你输入的有误!" ;
        }
    }
    /**
     * 功能:得到指定时间的前面,或者后面几分<br>
     * @param time (指定时间)
     * @param minutes (正数表示后面几分,负数表示前面几分)
     * @return String
     * @author Administrator
     */
    public String getTimeAddMinute(String time ,int minutes)
    {
        if(time.split(":").length == 3)
        {
            setTime(time);
            return  addTimeAll(Calendar.MINUTE,minutes);
        }else
        {
            return "时间格式为hh:mm:ss 你输入的有误!" ;
        }
    }
    /**
     * 功能:得到指定时间的前面,或者后面几小时<br>
     * @param time (指定时间)
     * @param hours (正数表示后面几小时,负数表示前面几小时)
     * @return String
     * @author Administrator
     */
    public String getTimeAddHour(String time ,int hours)
    {
        if(time.split(":").length == 3)
        {
            setTime(time);
            return  addTimeAll(Calendar.HOUR,hours);
        }else
        {
            return "日期格式为YYYY-MM-DD 你输入的有误!" ;
        }
    }
    /**
     * 功能:得到两个指定时间的时间差<br>
     * @param startTime 开始时间
     * @param endTime  结束时间
     * @return minusTime 时间差
     * @author Administrator
     */
    public String getTimeMinus(String startTime, String endTime)
    {
        String minusTime = null;
        if((startTime.split(":").length == 3) && (endTime.split(":").length == 3))
        {
         int time[] = null;
         if(startTime.compareTo(endTime)<0)
            {
          time = getTotalTime(startTime,endTime);
          minusTime = "+ " + getTotalTimeFormat(time[0],time[1],time[2]);
            }else
            {
             time = getTotalTime(endTime,startTime);
             minusTime = "- " + getTotalTimeFormat(time[0],time[1],time[2]);
            }
            return minusTime ;
        }else
        {
            return "日期格式为YYYY-MM-DD 你输入的有误!" ;
        }
    }
    /***
     *
     * 功能:得到两个指定日期的时间差 天数<br>
     *
     * @param startDate
     * @param endDate
     * @return
     * @author Administrator
     */
    public int getDateMinus(String startDate, String endDate)
    {
        int index = 0 ;
        if(startDate.compareTo(endDate)<0)
        {
         while( !getDateAddDay(startDate,index).equals( endDate))
         {      
             index += 1;
         }
        }else
        {
         while( !getDateAddDay(endDate,index).equals( startDate))
         {      
             index += 1;
         }
         index = -index;
        }
        return index;
    }
    /***
     * 返回传入的日期之间的所包含天数 List
     * @param startDate
     * @param endDate
     * @return
     */
    public ArrayList getDateMinusList(String startDate, String endDate)
    {
        int index = 0 ;
        ArrayList arrayDays = new ArrayList();
        if(startDate.compareTo(endDate)<0)
        {
         while( !getDateAddDay(startDate,index).equals( endDate))
         {      
          arrayDays.add(getDateAddDay(startDate,index));
          index += 1;
         }
         arrayDays.remove(0);
        }else
        {
         while( !getDateAddDay(endDate,index).equals( startDate))
         {      
          arrayDays.add(getDateAddDay(endDate,index));
          index += 1;
         }
         arrayDays.remove(0);
        }
        return arrayDays;
    }
    /**
     *
     * 功能:将从数据库中得到的时间差转换为标准的XX天XX小时XX分XX秒<br>
     *
     * @param DBTimeMinus
     * @return String formatDBTime
     * @author Administrator
     */
    public String getDBTimeMinus(String DBTimeMinus)
    {
        String formatDBTime = null ;
        double DBtime  = 0 ;
        try
        {
            DBtime = Double.parseDouble(DBTimeMinus);
        }catch(Exception e)
        {
            return "你输入的参数有误!" ;
        }
        int days = 0 ;
        int hours = 0 ;
        int minutes = 0 ;
        int seconds = 0 ;
        double x = 0 ;
        double y = 0 ;
        x = DBtime ;
        y = Math.floor(x);
        days = (int)y ;
        x = (x - y) * 24 ;           
        y = Math.floor(x);
        hours = (int)y ;
        x = (x - y) * 60 ;           
        y = Math.floor(x);
        minutes = (int)y ;
        x = (x - y) * 60 ;           
        y = Math.floor(x);
        seconds = (int)y ;
        formatDBTime = days + "天" + hours + "小时" + minutes + "分" + "" + seconds + "秒" ;
        return formatDBTime ;
    }
    /**
     *
     * 功能:将从数据库中得到的时间差转换为标准的XX天XX小时XX分XX秒<br>
     *
     * @param DBTimeMinus
     * @return String formatDBTime
     * @author Administrator
     */
    public String getDBTimeMinus(double DBTimeMinus)
    {
        String formatDBTime = null ;
        formatDBTime = getDBTimeMinus(String.valueOf(DBTimeMinus));
        return formatDBTime ;
    }
    /**
     *  功能 :给增加时间的方法提供支持
     *  @param  calendarProperty Calendar的各个属性
     *  @return String
     */
    private String addTimeAll(int calendarProperty, int property)
    {
        String strY = null;
        String strZ = null;
        localTime.add(calendarProperty,property);
        x = localTime.get(Calendar.HOUR);
        y = localTime.get(Calendar.MINUTE); 
        z = localTime.get(Calendar.SECOND);
        strY = y >= 10 ? String.valueOf(y) : ("0" + y) ;
        strZ = z >= 10 ? String.valueOf(z) : ("0" + z) ;
        return  x + ":" + strY + ":" + strZ;
    }
    /**
     *  功能 :给增加日期的方法提供支持
     *  @param  calendarProperty Calendar的各个属性
     *  @return String
     */
    private String addDateAll(int calendarProperty, int property)
    {
        String strY = null;
        String strZ = null;
        localTime.add(calendarProperty,property);
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1; 
        z = localTime.get(Calendar.DATE);
        strY = y >= 10 ? String.valueOf(y) : ("0" + y) ;
        strZ = z >= 10 ? String.valueOf(z) : ("0" + z) ;
        return  x + "-" + strY + "-" + strZ;
    }
    /**
     * 将获得的总的时间(小时,分钟,秒)数转换为String格式
     * @param hour int
     * @param minute int
     * @param second int
     * @return totalTime String
     */
    private String getTotalTimeFormat(int hour ,int minute, int second)
    {
        int nHour = 0;
        int nHourIn = 0;
        int nMinute = 0;
        int nMinuteIn = 0;
        int nSecond = 0;
        /*********************秒数 格式化*********************/
        if(second > 0 && second <60)
        {
            nSecond = second;
        }else
        {
            if(second < 0 )
            {
                nSecond = second%60 + 60 ;
                nMinuteIn = second/60 - 1 ;
            }
            if(second > 60 )
            {
                nSecond = second%60 ;
                nMinuteIn= second/60 ;
            }
        }
        /****************分 格式化**************************/
        if(nMinuteIn + minute > 0 && nMinuteIn + minute < 60)
        {
            nMinute = nMinuteIn + minute;
        }else
        {
            if(nMinuteIn+minute < 0)
            {
                nMinute = (nMinuteIn + minute)%60 + 60;
                nHourIn = (nMinuteIn + minute)/60 - 1 ;
            }
            if(nMinuteIn+minute > 60)
            {
                nMinute = (nMinuteIn + minute)%60 ;
                nHourIn = (nMinuteIn + minute)/60 ;
            }
        }
        /******************小时 格式化*************************/
        nHour = hour + nHourIn ;
       
        return nHour + "小时" + nMinute + "分钟"+nSecond + "秒";
    }
    /**
     * 获得时间差函数,即小时,分,秒的TotalTime
     * @param startTime String
     * @param endTime String
     * @return total int[]
     */
    private  int[] getTotalTime(String startTime , String endTime)
    {
        int[] total = new int[3];
           
        String timeOut [] = endTime.split(":") ;      
        String timeIn [] = startTime.split(":") ;
        for(int i = 0 ; i < 3 ; i++)
        {
           total[i] = Integer.parseInt(timeOut[i]) - Integer.parseInt(timeIn[i]);
        }    
        return total ;
    }
 /**
  * 日期转换 在 String ,Date ,Calendar之间相互转换
  *
  * @param obj  传入的需要转换的对象(String ,Date, Calendar)
  * @param toObj 需要转换成的对象 (String.class,Date.class,Calendar.class)
  * @return returnObject (返回的对象强制转换成与 toObj 一致)
  */
    public Object convert(Object obj, Class toObj)
    {
     Object returnObject = null;
     if(obj instanceof String)
     {
      if(((String)obj).split("-").length == 3)
      {
       if(toObj == Calendar.class)
       {
           setDate(obj.toString());
           returnObject = localTime;
       }
       if(toObj == Date.class)
       {
        setDate(obj.toString());
        returnObject = localTime.getTime();
       }
      }else
      {
       returnObject = "日期格式为YYYY-MM-DD 你输入的有误!";
      }
     } 
     if(obj instanceof Calendar)
     {
      if(toObj == String.class)
      {
             localTime = (Calendar)obj;
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
       returnObject = simpleDateFormat.format(localTime.getTime());
      }
      if(toObj == Date.class)
      {
       localTime = (Calendar)obj;
       returnObject = localTime.getTime();
      }
     }
     if(obj instanceof Date)
     {
      if(toObj == Calendar.class)
      {
          localTime.setTime((Date)obj);
          returnObject = localTime;
      }
      if(toObj == String.class)
      {
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
       returnObject = simpleDateFormat.format((Date)obj);
      }
     }
  return returnObject;
    }
    public static void main(String[] args)
    {
       //System.out.println( new ZrarDateTime().getDateMinus("2006-06-06","2009-06-09"));
       //System.out.println(new ZrarDateTime().getLocalDateTimeWeek());
//     String origin = "2009-06-06";
//     Date date = (Date)new ZrarDateTime().convert(origin,Date.class);
//     String strDate = (String)new ZrarDateTime().convert(date,String.class);
//     Calendar calDate = (Calendar)new ZrarDateTime().convert(strDate,Calendar.class);
//     date = (Date)new ZrarDateTime().convert(calDate,Date.class);
//     strDate = (String)new ZrarDateTime().convert(date,String.class);
//     System.out.println(strDate);
//     System.out.println(new ZrarDateTime().getLocalDateTime());
//     System.out.println(new ZrarDateTime().getLocalDateTimeWeek());
//     System.out.println(new ZrarDateTime().convert(new Date(),String.class));
//     System.out.println(new ZrarDateTime().getDateMinus("2002-09-06","2006-06-06"));
//     System.out.println(new ZrarDateTime().convert(new Date(),String.class));
     System.out.println(new ZrarDateTime().getDateMinusList("2006-06-13","2006-06-12"));
     
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值