日期:2014-05-19  浏览次数:20832 次

如何求当前时间的的第一天和最后一天的时间戳啊!
如题,补充一点,是为了求当前这一个月的数据的!
如果没有函数的话,我可能构造!
Date yes=new Date();
SimpleDateFormat sdf_y = new SimpleDateFormat("yyyy-MM");
Timestamp yes_from = Timestamp.valueOf(sdf_y.format(yes)+"-01 00:00:00");


Timestamp yes_to = Timestamp.valueOf(sdf_y.format(yes)+"-31 23:59:59");

但这样遇到一个月只有28,29,30天的就不行了!
求帮忙

------解决方案--------------------
http://blog.163.com/lizhenming_2008/blog/static/7655833320127734417906/
------解决方案--------------------
/**
 * 取得当前月第一天
 * @param date
 * @return
 * Date: 2012-5-14下午03:53:37
 */
public static Date getMonthFirst(Date date) {
try {
SimpleDateFormat sdfShort = new SimpleDateFormat(DATE_FORMAT_SHORT);
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
return sdfShort.parse(sdfYear.format(date) + "-" + sdfMonth.format(date) + "-01");
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}

/**
 * 取得当前月最后一天
 * @param date
 * @return
 * Date: 2012-5-14下午03:53:57
 */
public static Date getMonthLast(Date date) {
try {
SimpleDateFormat sdfShort = new SimpleDateFormat(DATE_FORMAT_SHORT);
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
int year = Integer.valueOf(sdfYear.format(date));
int month = Integer.valueOf(sdfMonth.format(date));
int day = getMonthDays(year, month);
return sdfShort.parse(year + "-" + month + "-" + day);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
------解决方案--------------------
/**
 * 取得某月天数
 * @param year
 * @param month
 * @return
 * Date: 2012-5-14下午04:13:41
 */
public static int getMonthDays(int year, int month) {
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);//把日期设置为当月第一天
a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
int maxDate = a.get(Calendar.DATE);
return maxDate;
}