자바 날짜 조작 방법

8552 단어 자바
이 글 은 아주 일찍 컴퓨터 에 저장 되 어 있 었 고...............................................................................
   
    
public class BusinessDate { 

    public BusinessDate() { 
    } 

    /** 
     *       ,   2009-02-11 
     * @return 
     */ 
    public static String getToday() { 
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd"); 
        Calendar cl = new GregorianCalendar(); 
        return sdf.format(cl.getTime()); 
    } 

    /** 
     *         ,   2009-02-11 23:9:21 
     * @return 
     */ 
    public static String getTodaytime() { 
        Calendar cl = new GregorianCalendar(); 
        return getToday() + " " + cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) + " "; 
    } 

    /** 
     *       ,   23:12:20 
     * @return 
     */ 
    public static String getTime() { 
        Calendar cl = new GregorianCalendar(); 
        return cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) + " "; 
    } 

    /** 
     *        
     * @return 
     */ 
    public static int getHour() { 
        Calendar cl = new GregorianCalendar(); 
        return cl.get(Calendar.HOUR_OF_DAY); 
    } 

    /** 
     *           20090211 
     * @return 
     */ 
    public static String getNoFormatToday() { 
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd"); 
        Calendar cl = new GregorianCalendar(); 
        return sdf.format(cl.getTime()); 
    } 

    /** 
     *           231611 
     * @return 
     */ 
    public static String getNoFormatTime() { 
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("HHmmss"); 
        Calendar cl = new GregorianCalendar(); 
        return sdf.format(cl.getTime()); 
    } 

    /** 
     *        
     * @return 
     */ 
    public static String getYear() { 
        return BusinessDate.getNoFormatToday().substring(0, 4); 
    } 

    /** 
     *        
     * @return 
     */ 
    public static String getMonth() { 
        return BusinessDate.getNoFormatToday().substring(4, 6); 
    } 

    /** 
     *       
     * @return 
     */ 
    public static String getDay() { 
        return BusinessDate.getNoFormatToday().substring(6,8   )   ; 
    } 

    /** 
     *           2009-02-10 
     * @return 
     */ 
    public static String getYesterday() { 
        String strYesterday = ""; 
        Calendar cale = null; 
        cale = new GregorianCalendar(); 
        cale.add(Calendar.DATE, -1); 
        strYesterday = BusinessDate.getStrByCalendar(cale); 
        return strYesterday; 
    } 

    public static String getStrByCalendar(Calendar cale) { 
        return (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(cale.getTime()); 
    } 

    /** 
     *           ,  "2009-02-11"   2009 2 11  
     * @param sDate 
     * @return 
     */ 
    public static String getChnDateString(String sDate) { 
        if (sDate == null) { 
            return null; 
        } 
        sDate = sDate.trim(); 
        if (sDate.length() == 7) { 
            sDate += "-01"; 
        } 
        StringTokenizer st = new StringTokenizer(sDate, "-"); 
        int year = 2100; 
        int month = 0; 
        int day = 1; 
        try { 
            year = Integer.parseInt(st.nextToken()); 
            month = Integer.parseInt(st.nextToken()) - 1; 
            day = Integer.parseInt(st.nextToken()); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        Calendar cl = new GregorianCalendar(year, month, day); 
        return cl.get(Calendar.YEAR) + " " + (cl.get(Calendar.MONTH) + 1) + " " + cl.get(Calendar.DATE) + " "; 
    } 

    /** 
     *             
     * @param year 
     * @param month 
     * @return 
     */ 
    public static String getMaxDayOfMonth(int year, int month) { 
        Calendar cal = new GregorianCalendar(year, month - 1, 1); 
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
        return sdf.format(cal.getTime()); 
    } 

    /** 
     *            
     * @param year 
     * @param month 
     * @return 
     */ 
    public static String getMinDayOfMonth(int year, int month) { 
        Calendar cal = new GregorianCalendar(year, month - 1, 1); 
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH)); 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
        return sdf.format(cal.getTime()); 
    } 

    /** 
     *          , 2006 11 28      
     * @return 
     */ 
    public static String getChineseToDay() { 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd  E", Locale.CHINESE); 
        Calendar cl = new GregorianCalendar(); 
        return sdf.format(cl.getTime()); 
    } 

    /** 
     *          , 2006 11 28        05:06 
     * @return 
     */ 
    public static String getChineseToDayTime() { 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd  E a", Locale.CHINESE); 
        Calendar cl = new GregorianCalendar(); 
        return sdf.format(cl.getTime()); 
    } 

    /** 
     *      ,      
     * @param sDate 
     * @return 
     */ 
    public static Calendar getDate(String sDate) { 
        if (sDate == null) { 
            return null; 
        } 
        sDate = sDate.trim(); 
        if (sDate.length() == 7) { 
            sDate += "-01"; 
        } 
        StringTokenizer st = new StringTokenizer(sDate, "-"); 
        int year = 2100; 
        int month = 0; 
        int day = 1; 
        try { 
            year = Integer.parseInt(st.nextToken()); 
            month = Integer.parseInt(st.nextToken()) - 1; 
            day = Integer.parseInt(st.nextToken()); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return new GregorianCalendar(year, month, day); 
    } 

    /** 
     *                 
     * @param sDate 
     * @return 
     */ 
    public static String getDateString(Calendar sDate) { 
        if (sDate == null) { 
            return ""; 
        } 
        return (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(sDate.getTime()); 
    } 

    /**               
     * @param sDate 
     * @return 
     */ 
    public static String getYearMonth(Calendar sDate) { 
        if (sDate == null) { 
            return ""; 
        } 
        return (new java.text.SimpleDateFormat("yyyy-MM")).format(sDate.getTime()); 
    } 
    
    /**            ,   (yyyy-mm-dd) 
     *   cale1  cale2,  1 
     *   cale1  cale2,  -1 
     *     ,  0 
     *       ,  -2 
     * @param cale1 
     * @param cale2 
     * @return 
     */ 
    public static int compareCalendar(String cale1, String cale2) { 
        Calendar calendar1 = getDate(cale1); 
        Calendar calendar2 = getDate(cale2); 
        if (calendar1 == null || calendar2 == null) { 
            return -2; 
        } 
        return calendar1.compareTo(calendar2); 
    } 
} 

좋은 웹페이지 즐겨찾기