常用Calendar 操作日期的方法

package date;

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

/**
 * 日期公用处理类
 * 
 * @author SongJun
 * @version 1.3
 */
public class DateUtil {
 public static void main(String args[]){
  System.out.println(DateUtil.getMonthList("2000-10", "2001-6").toString());
  System.out.println(DateUtil.getDayList("2000-8-1", "2000-8-10").toString());
  System.out.println(DateUtil.getYearList().toString());
  System.out.println(DateUtil.getCurrYear());
  Calendar   c   =   Calendar.getInstance();   
        c.set(Calendar.YEAR, 2010);
        c.set(Calendar.MONTH, 5);//11代表12月
        c.set(Calendar.DATE, 3);//时间重新设置成2008-1-18
  Date d = c.getTime();
  System.out.println(DateUtil.getWeekOfYear(d));
  System.out.println(DateUtil.getWeekCountInYear(2010));
  System.out.println(DateUtil.getFirstDayOfWeek(d));
  System.out.println(DateUtil.getFirstDayOfWeek(2008,3));//找第3行,的星期一
  
 }
 
    /**
     * 解析一个日期段之间的所有月份
     * 
     * @param beginDateStr
     * @param endDateStr
     * @return
     */
    public static ArrayList getMonthList(String beginDateStr, String endDateStr) {
        // 指定要解析的时间格式
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM");
        // 返回的月份列表
        String sRet = "";

        // 定义一些变量
        Date beginDate = null;
        Date endDate = null;

        GregorianCalendar beginGC = null;
        GregorianCalendar endGC = null;
        ArrayList list = new ArrayList();

        try {
            // 将字符串parse成日期
            beginDate = f.parse(beginDateStr);
            endDate = f.parse(endDateStr);

            // 设置日历
            beginGC = new GregorianCalendar();
            beginGC.setTime(beginDate);

            endGC = new GregorianCalendar();
            endGC.setTime(endDate);

            // 直到两个时间相同
            while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {//如果参数 Date 等于此 Date,则返回值 0;如果此 Date 在 Date 参数之前,则返回小于 0 的值;如果此 Date 在 Date 参数之后,则返回大于 0 的值。 
                sRet = beginGC.get(Calendar.YEAR) + "-"
                        + (beginGC.get(Calendar.MONTH) + 1);
                list.add(sRet);
                // 以月为单位,增加时间
                beginGC.add(Calendar.MONTH, 1);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解析一个日期段之间的所有日期
     * 
     * @param beginDateStr
     *            开始日期
     * @param endDateStr
     *            结束日期
     * @return
     */
    public static ArrayList getDayList(String beginDateStr, String endDateStr) {
        // 指定要解析的时间格式
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

        // 定义一些变量
        Date beginDate = null;
        Date endDate = null;

        Calendar beginGC = null;
        Calendar endGC = null;
        ArrayList list = new ArrayList();

        try {
            // 将字符串parse成日期
            beginDate = f.parse(beginDateStr);
            endDate = f.parse(endDateStr);

            // 设置日历
            beginGC = Calendar.getInstance();
            beginGC.setTime(beginDate);

            endGC = Calendar.getInstance();
            endGC.setTime(endDate);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            // 直到两个时间相同
            while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {

                list.add(sdf.format(beginGC.getTime()));
                // 以日为单位,增加时间
                beginGC.add(Calendar.DAY_OF_MONTH, 1);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 
     * 今年开始
     * @param 5  过去几年
     *            
     * @param 10  后来几年
     *            
     * @return
     */
    public static ArrayList getYearList() {
        ArrayList list = new ArrayList();
        Calendar c = null;
        c = Calendar.getInstance();
        c.setTime(new Date());
        int currYear = Calendar.getInstance().get(Calendar.YEAR);

        int startYear = currYear - 5;
        int endYear = currYear + 10;
        for (int i = startYear; i < endYear; i++) {
            list.add(new Integer(i));
        }
        return list;
    }
    //获得当年
    public static int getCurrYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }
    
    /**
     * 取得当前日期是多少周
     * 
     * @param date
     * @return
     */
    public static int getWeekOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.SUNDAY);//设置一星期的第一天是哪一天;既日历的开头是哪一天
        c.setMinimalDaysInFirstWeek(5);//设置一年中第一个星期所需的最少天数 ; 你自己开日历看看这年,头上有几个数字,设置一下;
        c.setTime(date);

        return c.get(Calendar.WEEK_OF_YEAR);//  哪个是第一周呢? 日历里的第1行;   得到的是这个Date 对象 ,位于日历的第几行
    }          

   
    
    /**
     * 得到一年的总周数
     * @param year
     * @return
     */
    public static int getWeekCountInYear(int year){
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        int count = getWeekOfYear(c.getTime());
        return count;
    }
    
    /**
     * 取得当前日期所在周的第一天
     * 
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday 它就会自动找这个星期的
        return c.getTime();
    }

    /**
     * 取得当前日期所在周的最后一天
     * 
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday 它就会自动找这个星期的
        return c.getTime();
    }

    /**
     * 得到某年某周的第一天
     * 
     * @param year
     * @param week
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getFirstDayOfWeek(cal.getTime());
    }

    /**
     * 得到某年某周的最后一天
     * 
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getLastDayOfWeek(cal.getTime());
    }
   

  
    
    /**
     * 得到某一年周的总数
     * 
     * @param year
     * @return
     */
    public static LinkedHashMap getWeekList(int year) {
        LinkedHashMap map = new LinkedHashMap();
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        int count = getWeekOfYear(c.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dayOfWeekStart = "";
        String dayOfWeekEnd = "";
        for (int i = 1; i <= count; i++) {
            dayOfWeekStart = sdf.format(getFirstDayOfWeek(year, i));//格式化 某年某周的第一天
            dayOfWeekEnd = sdf.format(getLastDayOfWeek(year, i));格式化 某年某周的最后一天
            map.put(new Integer(i), "第"+i+"周(从"+dayOfWeekStart + "至" + dayOfWeekEnd+")");
        }
        return map;

    }

  
    /**
     * 得到某年某月的第一天
     * @param year
     * @param month
     * @return
     */
    public static Date getFirestDayOfMonth(int year,int month){
        month = month-1;
        Calendar   c   =   Calendar.getInstance();   
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        
        int day = c.getActualMinimum(c.DAY_OF_MONTH);//返回指定日历字段可能拥有的最小值

        c.set(Calendar.DAY_OF_MONTH, day);
        return c.getTime();
        
    }
    
    /**
     * 提到某年某月的最后一天
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(int year,int month){
        month = month-1;
        Calendar   c   =   Calendar.getInstance();   
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        int day = c.getActualMaximum(c.DAY_OF_MONTH);
        c.set(Calendar.DAY_OF_MONTH, day);
        return c.getTime();
    }

    
}
 

你可能感兴趣的:(jdk,Calendar)