calendar操做詳解

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


/**
* 經常使用日曆操做輔助類
*  
* @author steven 2010-08-10
* @email:qing.tan@iwode.com
*/
public class CalendarUtil {  


    private int weeks = 0;// 用來全局控制 上一週,本週,下一週的週數變化  
    private int MaxDate; // 一月最大天數  
    private int MaxYear; // 一年最大天數  


    /**
     * 測試
     *  
     * @param args
     */
    public static void main(String[] args) {  
        CalendarUtil tt = new CalendarUtil();  
        System.out.println("獲取當天日期:" + tt.getNowTime("yyyy-MM-dd"));  
        System.out.println("獲取本週一日期:" + tt.getMondayOFWeek());  
        System.out.println("獲取本週日的日期~:" + tt.getCurrentWeekday());  
        System.out.println("獲取上週一日期:" + tt.getPreviousWeekday());  
        System.out.println("獲取上週日日期:" + tt.getPreviousWeekSunday());  
        System.out.println("獲取下週一日期:" + tt.getNextMonday());  
        System.out.println("獲取下週日日期:" + tt.getNextSunday());  
        System.out.println("得到相應周的週六的日期:" + tt.getNowTime("yyyy-MM-dd"));  
        System.out.println("獲取本月第一天日期:" + tt.getFirstDayOfMonth());  
        System.out.println("獲取本月最後一天日期:" + tt.getDefaultDay());  
        System.out.println("獲取上月第一天日期:" + tt.getPreviousMonthFirst());  
        System.out.println("獲取上月最後一天的日期:" + tt.getPreviousMonthEnd());  
        System.out.println("獲取下月第一天日期:" + tt.getNextMonthFirst());  
        System.out.println("獲取下月最後一天日期:" + tt.getNextMonthEnd());  
        System.out.println("獲取本年的第一天日期:" + tt.getCurrentYearFirst());  
        System.out.println("獲取本年最後一天日期:" + tt.getCurrentYearEnd());  
        System.out.println("獲取去年的第一天日期:" + tt.getPreviousYearFirst());  
        System.out.println("獲取去年的最後一天日期:" + tt.getPreviousYearEnd());  
        System.out.println("獲取明年第一天日期:" + tt.getNextYearFirst());  
        System.out.println("獲取明年最後一天日期:" + tt.getNextYearEnd());  
        System.out.println("獲取本季度第一天:" + tt.getThisSeasonFirstTime(11));  
        System.out.println("獲取本季度最後一天:" + tt.getThisSeasonFinallyTime(11));  
        System.out.println("獲取兩個日期之間間隔天數2008-12-1~2008-9.29:"
                + CalendarUtil.getTwoDay("2008-12-1", "2008-9-29"));  
        System.out.println("獲取當前月的第幾周:" + tt.getWeekOfMonth());  
        System.out.println("獲取當前年份:" + tt.getYear());  
        System.out.println("獲取當前月份:" + tt.getMonth());  
        System.out.println("獲取今天在本年的第幾天:" + tt.getDayOfYear());  
        System.out.println("得到今天在本月的第幾天(得到當前日):" + tt.getDayOfMonth());  
        System.out.println("得到今天在本週的第幾天:" + tt.getDayOfWeek());  
        System.out.println("得到半年後的日期:"
                + tt.convertDateToString(tt.getTimeYearNext()));  
    }  


    /**
     * 得到當前年份
     *  
     * @return
     */
    public static int getYear() {  
        return Calendar.getInstance().get(Calendar.YEAR);  
    }  


    /**
     * 得到當前月份
     *  
     * @return
     */
    public static int getMonth() {  
        return Calendar.getInstance().get(Calendar.MONTH) + 1;  
    }  


    /**
     * 得到今天在本年的第幾天
     *  
     * @return
     */
    public static int getDayOfYear() {  
        return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);  
    }  


    /**
     * 得到今天在本月的第幾天(得到當前日)
     *  
     * @return
     */
    public static int getDayOfMonth() {  
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);  
    }  


    /**
     * 得到今天在本週的第幾天
     *  
     * @return
     */
    public static int getDayOfWeek() {  
        return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);  
    }  


    /**
     * 得到今天是這個月的第幾周
     *  
     * @return
     */
    public static int getWeekOfMonth() {  
        return Calendar.getInstance().get(Calendar.DAY_OF_WEEK_IN_MONTH);  
    }  


    /**
     * 得到半年後的日期
     *  
     * @return
     */
    public static Date getTimeYearNext() {  
        Calendar.getInstance().add(Calendar.DAY_OF_YEAR, 183);  
        return Calendar.getInstance().getTime();  
    }  


    /**
     * 將日期轉換成字符串
     *  
     * @param dateTime
     * @return
     */
    public static String convertDateToString(Date dateTime) {  
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
        return df.format(dateTime);  
    }  


    /**
     * 獲得二個日期間的間隔天數
     *  
     * @param sj1
     * @param sj2
     * @return
     */
    public static String getTwoDay(String sj1, String sj2) {  
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
        long day = 0;  
        try {  
            java.util.Date date = myFormatter.parse(sj1);  
            java.util.Date mydate = myFormatter.parse(sj2);  
            day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
        } catch (Exception e) {  
            return "";  
        }  
        return day + "";  
    }  


    /**
     * 根據一個日期,返回是星期幾的字符串
     *  
     * @param sdate
     * @return
     */
    public static String getWeek(String sdate) {  
        // 再轉換爲時間  
        Date date = CalendarUtil.strToDate(sdate);  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        // int hour=c.get(Calendar.DAY_OF_WEEK);  
        // hour中存的就是星期幾了,其範圍 1~7  
        // 1=星期日 7=星期六,其餘類推  
        return new SimpleDateFormat("EEEE").format(c.getTime());  
    }  


    /**
     * 將短期格式字符串轉換爲時間 yyyy-MM-dd
     *  
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {  
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
        ParsePosition pos = new ParsePosition(0);  
        Date strtodate = formatter.parse(strDate, pos);  
        return strtodate;  
    }  


    /**
     * 兩個時間之間的天數
     *  
     * @param date1
     * @param date2
     * @return
     */
    public static long getDays(String date1, String date2) {  
        if (date1 == null || date1.equals(""))  
            return 0;  
        if (date2 == null || date2.equals(""))  
            return 0;  
        // 轉換爲標準時間  
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
        java.util.Date date = null;  
        java.util.Date mydate = null;  
        try {  
            date = myFormatter.parse(date1);  
            mydate = myFormatter.parse(date2);  
        } catch (Exception e) {  
        }  
        long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
        return day;  
    }  


    /**
     * 計算當月最後一天,返回字符串
     *  
     * @return
     */
    public String getDefaultDay() {  
        String str = "";  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  


        Calendar lastDate = Calendar.getInstance();  
        lastDate.set(Calendar.DATE, 1);// 設爲當前月的1號  
        lastDate.add(Calendar.MONTH, 1);// 加一個月,變爲下月的1號  
        lastDate.add(Calendar.DATE, -1);// 減去一天,變爲當月最後一天  


        str = sdf.format(lastDate.getTime());  
        return str;  
    }  


    /**
     * 上月第一天
     *  
     * @return
     */
    public String getPreviousMonthFirst() {  
        String str = "";  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  


        Calendar lastDate = Calendar.getInstance();  
        lastDate.set(Calendar.DATE, 1);// 設爲當前月的1號  
        lastDate.add(Calendar.MONTH, -1);// 減一個月,變爲下月的1號  
        // lastDate.add(Calendar.DATE,-1);//減去一天,變爲當月最後一天  


        str = sdf.format(lastDate.getTime());  
        return str;  
    }  


    /**
     * 獲取當月第一天
     *  
     * @return
     */
    public String getFirstDayOfMonth() {  
        String str = "";  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  


        Calendar lastDate = Calendar.getInstance();  
        lastDate.set(Calendar.DATE, 1);// 設爲當前月的1號  
        str = sdf.format(lastDate.getTime());  
        return str;  
    }  


    /**
     * 得到本週星期日的日期
     *  
     * @return
     */
    public String getCurrentWeekday() {  
        weeks = 0;  
        int mondayPlus = this.getMondayPlus();  
        GregorianCalendar currentDate = new GregorianCalendar();  
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);  
        Date monday = currentDate.getTime();  


        DateFormat df = DateFormat.getDateInstance();  
        String preMonday = df.format(monday);  
        return preMonday;  
    }  


    /**
     * 獲取當天時間
     *  
     * @param dateformat
     * @return
     */
    public String getNowTime(String dateformat) {  
        Date now = new Date();  
        SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);// 能夠方便地修改日期格式  
        String hehe = dateFormat.format(now);  
        return hehe;  
    }  


    /**
     * 得到當前日期與本週日相差的天數
     *  
     * @return
     */
    private int getMondayPlus() {  
        Calendar cd = Calendar.getInstance();  
        // 得到今天是一週的第幾天,星期日是第一天,星期二是次日......  
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 由於按中國禮拜一做爲第一天因此這裏減1  
        if (dayOfWeek == 1) {  
            return 0;  
        } else {  
            return 1 - dayOfWeek;  
        }  
    }  


    /**
     * 得到本週一的日期
     *  
     * @return
     */
    public String getMondayOFWeek() {  
        weeks = 0;  
        int mondayPlus = this.getMondayPlus();  
        GregorianCalendar currentDate = new GregorianCalendar();  
        currentDate.add(GregorianCalendar.DATE, mondayPlus);  
        Date monday = currentDate.getTime();  


        DateFormat df = DateFormat.getDateInstance();  
        String preMonday = df.format(monday);  
        return preMonday;  
    }
相關文章
相關標籤/搜索