静以修心

JAVA 简单的日期封装类

本来以为有了date4j就万事无休了,结果在工作的时候发觉有不少脚步仅仅需要两三个简单的class执行一下就可以完成任务了。也就是说即使是date4j,相对于这两三个甚至是一个class来说还是过于臃肿了。于是乎自己写了个简单的日期封装类。
主要功能是
1.获取当前时间
2.获取当前年,月,日,时,分,秒
3.获取指定日期的年,月,日,时,分,秒
4.获取两个日期的时间差(包括年月日时分秒)
5.将字符窜类型转成java.util.date类型
6.指定日期添加时间

package com.kohri.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @descriped a simple class for date
 * 
@author Kohri
 * @date 2012/4/22
 * 
@version 1.0
 
*/
public class SimpleDate {

    private static String defaultFormat = "yyyy-MM-dd HH:mm:ss";
    private static SimpleDateFormat sf = null ;
    private static Calendar cal = Calendar.getInstance();
    private static Date date = null;

    /**
     * get current date time (default 'yyyy-MM-dd HH:mm:ss')
     * 
@return string
     
*/
    public static String getDateNow() {
        sf = new SimpleDateFormat(defaultFormat);
        String currentTime = "";
        currentTime = sf.format(new Date());
        return currentTime;
    }
    
    /**
     * get year (default now) 
     * 
@return int 
     
*/
    public static int getYear(){
        int currentYear = cal.get(Calendar.YEAR);
        return currentYear;
    }
    
    /**
     * get mon (default now) 
     * 
@return int 
     
*/
    public static int getMonth(){
        int currentMonth = cal.get(Calendar.MONTH) + 1;
        return currentMonth;
    }
    
    /**
     * get day of month (default now)
     * 
@return int 
     
*/
    public static int getDay(){
        int currentDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
        return currentDayOfMonth;
    }
    
    /**
     * get hours (default now)
     * 
@return int 
     
*/
    public static int getHours(){
        int currentHours = cal.get(Calendar.HOUR_OF_DAY);
        return currentHours;
    }
    
    /**
     * get  minutes (default now)
     * 
@return int 
     
*/
    public static int getMinutes(){
        int currentMinute = cal.get(Calendar.MINUTE);
        return currentMinute;
    }

    /**
     * get seconds (default now)
     * 
@return int 
     
*/
    public static int getSeconds(){
        int currentSecond = cal.get(Calendar.SECOND);
        return currentSecond;
    }
    
    /**
     * string change to date
     * 
@param strDate
     * 
@param dateFormat
     * 
@return date
     
*/
    public static Date toDate(String strDate, String dateFormat){
        if(strDate == null || strDate.length() == 0){
            return null;
        }
        Date date = null;
        DateFormat df = new SimpleDateFormat(dateFormat);
        try {
            date = df.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    
    /**
     * Returns this Calendar's time value in milliseconds
     * 
@param p_date
     * 
@return long
     
*/
    public static long getMillisOfDate(Date date) {
           cal.setTime(date);
           return cal.getTimeInMillis();
    }
    
    
    
    /**
     * compare two date 
     * return the greater date 
     * if equals return null
     * 
@param strStartDate
     * 
@param strEndDate
     * 
@return date 
     
*/
    public static Date getGreaterDate(String strStartDate, String strEndDate){
        Date date = null;
        Date startDate = toDate(strStartDate, "yyyy-MM-dd");
        Date endDate = toDate(strEndDate, "yyyy-MM-dd");
        long startTime = getMillisOfDate(startDate);
        long endTime = getMillisOfDate(endDate);
        if((startTime - endTime) > 0){
            return startDate;
        }else if((endTime - startTime) > 0){
            return endDate;
        }
        return date;
    }
    
    /**
     * get days between 2 different date
     * 
@param strStartDate less date (yyyy-MM-dd)
     * 
@param strEndDate greater date (yyyy-MM-dd)
     * 
@return long
     
*/
    public static long getDaysOftwoDiffDate(String strStartDate, String strEndDate){
           Date startDate = toDate(strStartDate, "yyyy-MM-dd");
           Date endDate = toDate(strEndDate, "yyyy-MM-dd");
           long startTime = getMillisOfDate(startDate);
           long endTime = getMillisOfDate(endDate);
           long betweenDays = (long) ((endTime - startTime) / ( 1000 * 60 * 60 * 24 ));
           return betweenDays;
    }
    
    /**
     * get weeks between 2 different date
     * 
@param strStartDate less date (yyyy-MM-dd)
     * 
@param strEndDate greater date (yyyy-MM-dd)
     * 
@return long
     
*/
    public static long getWeeksOfTwoDiffDate(String strStartDate, String strEndDate){
        return getDaysOftwoDiffDate(strStartDate, strEndDate) / 7;
    }
    
    /**
     * get months between 2 different date
     * 
@param strStartDate less date (yyyy-MM-dd)
     * 
@param strEndDate greater date (yyyy-MM-dd)
     * 
@return long
     
*/
    public static long getMonthsOfTwoDiffDate(String strStartDate, String strEndDate){
        return getDaysOftwoDiffDate(strStartDate, strEndDate) / 30;
    }
    
    /**
     * get years between 2 different date
     * 
@param strStartDate less date (yyyy-MM-dd)
     * 
@param strEndDate greater date (yyyy-MM-dd)
     * 
@return long
     
*/
    public static long getYearsOfTwoDiffDate(String strStartDate, String strEndDate){
        return getDaysOftwoDiffDate(strStartDate, strEndDate) / 365;
    }
    
    /**
     * add date
     * 
@param date
     * 
@param count 
     * 
@param field Calendar.YEAR(MONTH..)
     * 
@param format DateFormat(yyyy-MM-dd)
     * 
@return string
     
*/
    public static String addDate(Date date,int count,int field,String format){
           cal.setTime(date);
           int year = getYear();
           int month = getMonth() - 1;
           int day = getDay();
           int hours = getHours();
           int minutes = getMinutes();
           int seconds = getSeconds();
           Calendar calendar = new GregorianCalendar(year, month, day, hours, minutes, seconds);
           calendar.add(field,count);
           DateFormat df = new SimpleDateFormat(format);
           String tmpDate = df.format(calendar.getTime());
           if(date == null){
cal.setTime(new Date());
}else{
cal.setTime(date);
}
           return tmpDate;
    }
    
    /**
     * calendar settime
     * 
@param date
     
*/
    private static void setCalTime(Date date){
        if(date != null){
            cal.setTime(date);
        }
    }
    
    //setter getter

    public static String getDefaultFormat() {
        return defaultFormat;
    }

    public static void setDefaultFormat(String defaultFormat) {
        SimpleDate.defaultFormat = defaultFormat;
    }

    public static Date getDate() {
        return date;
    }

    public static void setDate(Date date) {
        SimpleDate.date = date;
        setCalTime(date);
    }
    

}

posted on 2012-04-25 22:36 kohri 阅读(1916) 评论(1)  编辑  收藏 所属分类: JAVA

评论

# re: JAVA 简单的日期封装类 2012-04-28 09:12 jackzlz

可以考虑再加上获得当月的第一天和当月的最后一天  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2012年4月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

统计

常用链接

留言簿

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜