JAVA中用CALENDAR类计算周和周的起始日期(转)

JAVA中用CALENDAR类计算周和周的起始日期(转)
  1 package com.tongyue.hot.web;
  2 
  3 import java.util.Calendar;
  4 import java.util.Date;
  5 import java.util.GregorianCalendar;
  6 
  7 public class Dateutil {
  8 
  9  /** 
 10  * 取得当前日期是多少周 
 11  * 
 12  * @param date 
 13  * @return 
 14  */ 
 15  public static int getWeekOfYear(Date date) { 
 16  Calendar c = new GregorianCalendar(); 
 17  c.setFirstDayOfWeek(Calendar.MONDAY); 
 18  c.setMinimalDaysInFirstWeek(7); 
 19  c.setTime (date);
 20 
 21  return c.get(Calendar.WEEK_OF_YEAR); 
 22  }
 23 
 24  /** 
 25  * 得到某一年周的总数 
 26  * 
 27  * @param year 
 28  * @return 
 29  */ 
 30  public static int getMaxWeekNumOfYear(int year) { 
 31  Calendar c = new GregorianCalendar(); 
 32  c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
 33 
 34  return getWeekOfYear(c.getTime()); 
 35  }
 36 
 37  /** 
 38  * 得到某年某周的第一天 
 39  * 
 40  * @param year 
 41  * @param week 
 42  * @return 
 43  */ 
 44  public static Date getFirstDayOfWeek(int year, int week) { 
 45  Calendar c = new GregorianCalendar(); 
 46  c.set(Calendar.YEAR, year); 
 47  c.set (Calendar.MONTH, Calendar.JANUARY); 
 48  c.set(Calendar.DATE, 1);
 49 
 50  Calendar cal = (GregorianCalendar) c.clone(); 
 51  cal.add(Calendar.DATE, week * 7);
 52 
 53  return getFirstDayOfWeek(cal.getTime ()); 
 54  }
 55 
 56  /** 
 57  * 得到某年某周的最后一天 
 58  * 
 59  * @param year 
 60  * @param week 
 61  * @return 
 62  */ 
 63  public static Date getLastDayOfWeek(int year, int week) { 
 64  Calendar c = new GregorianCalendar(); 
 65  c.set(Calendar.YEAR, year); 
 66  c.set(Calendar.MONTH, Calendar.JANUARY); 
 67  c.set(Calendar.DATE, 1);
 68 
 69  Calendar cal = (GregorianCalendar) c.clone(); 
 70  cal.add(Calendar.DATE , week * 7);
 71 
 72  return getLastDayOfWeek(cal.getTime()); 
 73  }
 74 
 75  /** 
 76  * 取得指定日期所在周的第一天 
 77  * 
 78  * @param date 
 79  * @return 
 80  */ 
 81  public static Date getFirstDayOfWeek(Date date) { 
 82  Calendar c = new GregorianCalendar(); 
 83  c.setFirstDayOfWeek(Calendar.MONDAY); 
 84  c.setTime(date); 
 85  c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday 
 86  return c.getTime (); 
 87  }
 88 
 89  /** 
 90  * 取得指定日期所在周的最后一天 
 91  * 
 92  * @param date 
 93  * @return 
 94  */ 
 95  public static Date getLastDayOfWeek(Date date) { 
 96  Calendar c = new GregorianCalendar(); 
 97  c.setFirstDayOfWeek(Calendar.MONDAY); 
 98  c.setTime(date); 
 99  c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday 
100  return c.getTime(); 
101  } 
102  
103  /** 
104  * 取得当前日期所在周的第一天 
105  * 
106  * @param date 
107  * @return 
108  */ 
109  public static Date getFirstDayOfWeek() { 
110  Calendar c = new GregorianCalendar(); 
111  c.setFirstDayOfWeek(Calendar.MONDAY); 
112  c.setTime(new Date()); 
113  c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday 
114  return c.getTime (); 
115  }
116 
117  /** 
118  * 取得当前日期所在周的最后一天 
119  * 
120  * @param date 
121  * @return 
122  */ 
123  public static Date getLastDayOfWeek() { 
124  Calendar c = new GregorianCalendar(); 
125  c.setFirstDayOfWeek(Calendar.MONDAY); 
126  c.setTime(new Date()); 
127  c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday 
128  return c.getTime(); 
129  } 
130  /*
131   * 
132   
133  public static void main(String[] args) { 
134   int year = 2009; 
135   int week = 1;
136 
137   // 以2006-01-02位例 
138   Calendar c = new GregorianCalendar(); 
139   c.set(2009, Calendar.DECEMBER, 7); 
140   Date d = c.getTime();
141 
142   System.out.println("current date = " + d); 
143   System.out.println("getWeekOfYear = " + getWeekOfYear(d)); 
144   System.out.println("getMaxWeekNumOfYear = " + getMaxWeekNumOfYear(year)); 
145   System.out.println("getFirstDayOfWeek = " + getFirstDayOfWeek(year, week)); 
146   System.out.println("getLastDayOfWeek = " + getLastDayOfWeek(year, week)); 
147   System.out.println ("getFirstDayOfWeek = " + getFirstDayOfWeek(d)); 
148   System.out.println("getLastDayOfWeek = " + getLastDayOfWeek(d)); 
149   System.out.println ("getFirstDayOfWeek = " + getFirstDayOfWeek()); 
150   System.out.println("getLastDayOfWeek = " + getLastDayOfWeek()); 
151   }
152  */ 
153 }
JAVA中用CALENDAR类计算周和周的起始日期(转)

 2、Java获取当月的第一天和最后一天  

JAVA中用CALENDAR类计算周和周的起始日期(转)
 1 Calendar cal = Calendar.getInstance();   
 2   
 3 SimpleDateFormat datef=new SimpleDateFormat("yyyy-MM-dd");  
 4 //当前月的最后一天     
 5 cal.set( Calendar.DATE, 1 );  
 6 cal.roll(Calendar.DATE, - 1 );  
 7 Date endTime=cal.getTime();  
 8 String endTime1=datef.format(endTime)+" 23:59:59";  
 9  //当前月的第一天            
10 cal.set(GregorianCalendar.DAY_OF_MONTH, 1);   
11  Date beginTime=cal.getTime();  
12 String beginTime1=datef.format(beginTime)+" 00:00:00";
JAVA中用CALENDAR类计算周和周的起始日期(转)

 3 获取年的第一天和最后一天

JAVA中用CALENDAR类计算周和周的起始日期(转)
 1 /**
 2      * 获取某年第一天日期
 3      * @param year 年份
 4      * @return Date
 5      */
 6     public static Date getCurrYearFirst(int year){
 7         Calendar calendar = Calendar.getInstance();
 8         calendar.clear();
 9         calendar.set(Calendar.YEAR, year);
10         Date currYearFirst = calendar.getTime();
11         return currYearFirst;
12     }
13     
14     /**
15      * 获取某年最后一天日期
16      * @param year 年份
17      * @return Date
18      */
19     public static Date getCurrYearLast(int year){
20         Calendar calendar = Calendar.getInstance();
21         calendar.clear();
22         calendar.set(Calendar.YEAR, year);
23         calendar.roll(Calendar.DAY_OF_YEAR, -1);
24         Date currYearLast = calendar.getTime();
25         
26         return currYearLast;
27     }
JAVA中用CALENDAR类计算周和周的起始日期(转)

JAVA中用CALENDAR类计算周和周的起始日期(转)

上一篇:as 静态成员与实例成员:


下一篇:uva 10014 - Simple calculations