Java日付共通クラス

4348 ワード

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author [email protected]
 *
 * TODO               ,       -     - Java -      -     
 */
public class DateUtil {
    public static final int MILLISECONDS_PER_SECOND = 1000;
    public static final int MILLISECONDS_PER_DAY = 86400000;
    public static final int MILLISECONDS_PER_HOUR = 3600000;
    public static final int MILLISECONDS_PER_MINUTE = 60000;

    /*
     *              
     */
    public static String dateToStr(String aMask,Date date){
        SimpleDateFormat df = new SimpleDateFormat(aMask);
        String dateAsString = df.format(date);
        return dateAsString;
    }
    public static String getCurrentTime(){
        return getToday("HH:mm:ss");
    }
    /*
     *                  
     */
    public static String getToday(String aMask){
        Date today = new Date();
        String todayAsString = dateToStr(aMask,today);
        return todayAsString;
    }
    /*
     *                  
     */
    public static String getToday(){
        return getToday("yyyy-MM-dd");
    }
    /*
     *             Date
     */
    public static Date strToDate(String aMask,String strDate) {
        SimpleDateFormat format = new SimpleDateFormat(aMask);
        Date date = null;
        try {
            date = format.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    /*
     *             Date
     */
    public static Date strToDate(String strDate) {
        return strToDate("yyyy-MM-dd",strDate);
    }
    /*
     *           
     */
    public static long sub(String aMask,String strBeginDate, String strEndDate) {
        long dateRange=0;
        int num = 0;
        Date beginDate = strToDate(aMask,strBeginDate);
        Date endDate = strToDate(aMask,strEndDate);
        dateRange = endDate.getTime() - beginDate.getTime();
        return dateRange;
    }
    /*
     *           
     */
    public static long sub(String strBeginDate, String strEndDate) {
        long dateRange=0;
        int num = 0;
        Date beginDate = strToDate(strBeginDate);
        Date endDate = strToDate(strEndDate);
        dateRange = endDate.getTime() - beginDate.getTime();
        return dateRange;
    }
    /*
     *          
     */
    public static String subToDay(String strBeginDate, String strEndDate){
        String dayNum = "";
        long dateRange = sub(strBeginDate,strEndDate);
        dayNum = ""+(dateRange/MILLISECONDS_PER_DAY);
        return dayNum;
    }
    /*
     *          
     */
    public static String subToSecond(String aMask,String strBeginDate, String strEndDate){
        String secNum = "";
        long dateRange = sub(aMask,strBeginDate,strEndDate);
        secNum = ""+(dateRange/MILLISECONDS_PER_SECOND);
        return secNum;
    }
    public static String subToSecond(String strBeginDate, String strEndDate){
        String secNum = "";
        long dateRange = sub("yyyy-MM-dd HH:mm:ss",strBeginDate,strEndDate);
        secNum = ""+(dateRange/MILLISECONDS_PER_SECOND);
        return secNum;
    }
    public static void main(String[] args) {
        System.out.println("           :"+getToday());
        System.out.println("           :"+getToday("yyyy-MM-dd HH:mm:ss"));
        System.out.println("      :"+subToDay("2006-08-02 23:02:01", "2006-08-03 01:02:01"));
        System.out.println("      :"+subToSecond("2006-08-02 23:02:01", "2006-08-03 01:02:01"));
        System.out.println("    :"+getCurrentTime());
    }
}