浏览 15525 次
锁定老帖子 主题:Java 日期处理 技巧
精华帖 (0) :: 良好帖 (0) :: 新手帖 (15) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-06-13
最后修改:2009-06-13
假设我想知道这个礼拜的周一,月底,月初的具体时间,五天前又是几号?因此专门写了个类封装了下。其实如果组合使用其内部函数,还是很方便的。 需要注意的是这里的函数返回日期都是精确到秒的。 getDayBegine()的结果是: Mon Jun 08 00:00:00 CST 2009 形式的 而 getDayEnd()的结果为: Sun Jun 07 23:59:59 CST 2009 形式 废话不说,看代码(详单简单) package com.snail.gequ.action; import java.util.Calendar; import java.util.Date; import org.apache.log4j.Logger; import org.hibernate.criterion.Expression; import org.hibernate.criterion.Projections; import com.snail.commons.basedao.IBaseDAO; import com.snail.component.beans.Gequ; import com.snail.component.beans.Gequzhuanji; import com.snail.component.beans.Huiyuan; import com.snail.component.beans.Renqirecord; public class RenQiRecordUtil { private static final long serialVersionUID = 3193922028309094171L; private final static Logger logger = Logger .getLogger(RenQiRecordUtil.class); protected Integer getMonthOfThisYear() { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); return cal.get(Calendar.MONTH) + 1; } protected Date getTimeByMonth(Integer month) { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.set(Calendar.MONTH, month - 1); return cal.getTime(); } protected Date getMonthBegin(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0); return cal.getTime(); } protected Date getMonthEnd(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal .set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, 1, 0, 0, 0); cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) - 1); return cal.getTime(); } protected Date getWeekBegin(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); Date mm=nDaysAgo(cal.get(Calendar.DAY_OF_WEEK)-2,date); return getDayBegin(mm); } protected Date getWeekEnd(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); Date mm=nDaysAfter(cal.get(8-Calendar.DAY_OF_WEEK),date); return getDayEnd(mm); } protected Date nDaysAfter(int n,Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)+n); return cal.getTime(); } protected Date getDayBegin(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); return cal.getTime(); } protected Date getDayEnd(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal .set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH)+1, 0, 0, 0); cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) - 1); return cal.getTime(); } protected Date nMonthsAgo(Integer n,Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - n); return cal.getTime(); } protected Date nDaysAgo(Integer n,Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) -n); return cal.getTime(); } public RenQiRecordUtil() { // TODO Auto-generated constructor stub } } 恩 现在我们来看下一个例子: 如果我要拿到上个礼拜的第一天,怎么拿? RenQiRecordUtil util=new RenQiRecordUtil(); System.out.println(util.nDaysAgo(7,util.getWeekBegin(new Date()))); 那如果要拿到上个笔来的周末呢? System.out.println(util.getDayEnd(util.nDaysAgo(1,util.getWeekBegin(new Date())))); 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-06-13
比较实用可以作为工具类使用,平时都是用的时候再自己写一个,收藏了
|
|
返回顶楼 | |
发表时间:2009-06-16
估计作者是没看过jdk文档的,我就不说什么了.
|
|
返回顶楼 | |
发表时间:2009-06-16
一遇到有些关于日期的东西就烦。。。。。
各种奇怪的数据格式,能把人烦死。 |
|
返回顶楼 | |
发表时间:2009-09-01
getTimeByMonth(intger mouth) 这个方法的作用我不懂,到底什么意思呢?
|
|
返回顶楼 | |
发表时间:2009-09-01
|
|
返回顶楼 | |
发表时间:2009-10-14
Calendar 类用起相当地繁琐。
更可气的是 Date类里本来不错的几个方法全被他 过时 了 |
|
返回顶楼 | |
发表时间:2010-02-24
joda-time
|
|
返回顶楼 | |