简易代码之家

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  157 Posts :: 2 Stories :: 57 Comments :: 0 Trackbacks
    下面的DateUtil类为笔者在以前项目中用到的对日期处理的公用方法,请看到这个类的有兴趣的读者测试,如有BUG请留言,笔者也会在以后的项目中积累完善并更新到此类中去。

  1import java.text.SimpleDateFormat;
  2import java.util.Calendar;
  3import java.util.Date;
  4import java.util.GregorianCalendar;
  5
  6/**
  7 * 
  8 * @author zzk
  9 *
 10 */

 11public class DateUtil {
 12
 13    /**
 14     * 取得某天相加(减)後的那一天
 15     * 
 16     * @param date
 17     * @param num(可正可负)
 18     * @return
 19     */

 20    public static Date getAnotherDate(Date date, int num) {
 21        Calendar c = Calendar.getInstance();
 22        c.setTime(date);
 23        c.add(Calendar.DAY_OF_YEAR, num);
 24        return c.getTime();
 25    }

 26
 27    /**
 28     * 取得某月的的最后一天
 29     * 
 30     * @param year
 31     * @param month
 32     * @return
 33     */

 34    public static Date getLastDayOfMonth(int year, int month) {
 35        Calendar cal = Calendar.getInstance();
 36        cal.set(Calendar.YEAR, year);// 年
 37        cal.set(Calendar.MONTH, month - 1);// 月,因为Calendar里的月是从0开始,所以要减1
 38        cal.set(Calendar.DATE, 1);// 日,设为一号
 39        cal.add(Calendar.MONTH, 1);// 月份加一,得到下个月的一号
 40        cal.add(Calendar.DATE, -1);// 下一个月减一为本月最后一天
 41        return cal.getTime();// 获得月末是几号
 42    }

 43
 44    /**
 45     * 取得某天是一年中的多少周
 46     * 
 47     * @param date
 48     * @return
 49     */

 50    public static int getWeekOfYear(Date date) {
 51        Calendar c = new GregorianCalendar();
 52        c.setFirstDayOfWeek(Calendar.MONDAY);
 53        c.setMinimalDaysInFirstWeek(7);
 54        c.setTime(date);
 55        return c.get(Calendar.WEEK_OF_YEAR);
 56    }

 57
 58    /**
 59     * 取得某天所在周的第一天
 60     * 
 61     * @param date
 62     * @return
 63     */

 64    public static Date getFirstDayOfWeek(Date date) {
 65        Calendar c = new GregorianCalendar();
 66        c.setFirstDayOfWeek(Calendar.MONDAY);
 67        c.setTime(date);
 68        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
 69        return c.getTime();
 70    }

 71
 72    /**
 73     * 取得某天所在周的最后一天
 74     * 
 75     * @param date
 76     * @return
 77     */

 78    public static Date getLastDayOfWeek(Date date) {
 79        Calendar c = new GregorianCalendar();
 80        c.setFirstDayOfWeek(Calendar.MONDAY);
 81        c.setTime(date);
 82        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
 83        return c.getTime();
 84    }

 85
 86    /**
 87     * 取得某一年共有多少周
 88     * 
 89     * @param year
 90     * @return
 91     */

 92    public static int getMaxWeekNumOfYear(int year) {
 93        Calendar c = new GregorianCalendar();
 94        c.set(year, Calendar.DECEMBER, 31235959);
 95        return getWeekOfYear(c.getTime());
 96    }

 97    
 98    /**
 99     * 取得某年某周的第一天
100     * 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-05为2009年第一周的第一天
101     * @param year
102     * @param week
103     * @return
104     */

105    public static Date getFirstDayOfWeek(int year, int week) {
106        Calendar calFirst = Calendar.getInstance();
107        calFirst.set(year, 07);
108        Date firstDate = getFirstDayOfWeek(calFirst.getTime());
109
110        Calendar firstDateCal = Calendar.getInstance();
111        firstDateCal.setTime(firstDate);
112
113        Calendar c = new GregorianCalendar();
114        c.set(Calendar.YEAR, year);
115        c.set(Calendar.MONTH, Calendar.JANUARY);
116        c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));
117
118        Calendar cal = (GregorianCalendar) c.clone();
119        cal.add(Calendar.DATE, (week - 1* 7);
120        firstDate = getFirstDayOfWeek(cal.getTime());
121
122        return firstDate;
123    }

124
125    /**
126     * 取得某年某周的最后一天
127     * 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-04为2008年最后一周的最后一天
128     * @param year
129     * @param week
130     * @return
131     */

132    public static Date getLastDayOfWeek(int year, int week) {
133        Calendar calLast = Calendar.getInstance();
134        calLast.set(year, 07);
135        Date firstDate = getLastDayOfWeek(calLast.getTime());
136
137        Calendar firstDateCal = Calendar.getInstance();
138        firstDateCal.setTime(firstDate);
139
140        Calendar c = new GregorianCalendar();
141        c.set(Calendar.YEAR, year);
142        c.set(Calendar.MONTH, Calendar.JANUARY);
143        c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));
144
145        Calendar cal = (GregorianCalendar) c.clone();
146        cal.add(Calendar.DATE, (week - 1* 7);
147        Date lastDate = getLastDayOfWeek(cal.getTime());
148        
149        return lastDate;
150    }

151}

152
posted on 2008-01-08 18:49 Jakin.zhou 阅读(2901) 评论(6)  编辑  收藏

Feedback

# re: Java中对日期的常用处理 2008-01-08 22:01 xidudui
太赞了  回复  更多评论
  

# re: Java中对日期的常用处理[未登录] 2008-01-08 23:23 Crying
好贴 转啦
  回复  更多评论
  

# re: Java中对日期的常用处理 2008-01-09 09:23 隔叶黄莺
不错,我这里只说 取某年某月最后一天的写法,用下面的方式会好理解来,省得加加减减的
public static Date getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);// 年
cal.set(Calendar.MONTH, month - 1);// 月,因为Calendar里的月是从0开始,所以要减1
//设置该月实际的最后一天
cal.set(Calendar.DATE,cal.getActualMaximum(Calendar.DATE));
return cal.getTime();// 获得月末是几号
}

其他方法中看是否可以应用的上
getActualMaximum(int field)
getActualMinimum(int field)
这两个方法  回复  更多评论
  

# re: Java中对日期的常用处理 2008-01-14 12:02 Jakin.zhou
谢谢楼上朋友,这个方法确实不错。  回复  更多评论
  

# re: Java中对日期的常用处理[未登录] 2008-11-21 08:49 dimple
请问取出某月的三个月后是什么月份,
某月的六个月后是几月怎么取啊? 谢谢!  回复  更多评论
  

# re: Java中对日期的常用处理[未登录] 2008-11-25 16:04 dimple
这个已经解决@dimple
  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: