本人想作一个日期的判断现在是想取出当天所在周的周一日期:
GregorianCalendar cl = new GregorianCalendar();
cl.roll(Calendar.DATE, (cl.get(Calendar.DAY_OF_WEEK) - 1) * -1);
发现 int Month = cl.get(Calendar.MONTH) + 1;
这个方法取出来的月份还是7月;也就是说,只有日期回滚了,月并没有回滚.举个例子:
今天是7月1日,回滚到本周一应该是6月26号,可是,取出来的是7月26号.请教,如何能解决这个问题??
GregorianCalendar cl = new GregorianCalendar();
cl.roll(Calendar.DATE, (cl.get(Calendar.DAY_OF_WEEK) - 1) * -1);
发现 int Month = cl.get(Calendar.MONTH) + 1;
这个方法取出来的月份还是7月;也就是说,只有日期回滚了,月并没有回滚.举个例子:
今天是7月1日,回滚到本周一应该是6月26号,可是,取出来的是7月26号.请教,如何能解决这个问题??
解决方案 »
- SEVERE: Exception starting filter struts2 Unable to load conf本地上运行正常,链接到服务器上面就报错
- java打印
- 100求解决方案,用过hibernate+struts+spring的请进
- 新人求助,关于JSP页面连接ORCL 注册登录的问题
- xfire 问题
- 关于用jsp文件读xml文档的问题
- 可嵌入到JSP页面中的文本编辑器哪有下载,需要具有类似word的一些编辑功能
- 谁知道iText有没有使得生成PDF后。。。。。。
- 下学期毕业设计,选了个课题是关于WEB GIS的,想用JAVA(JSP)和MAPINFO开发,不知道行不行?
- 求助大佬救救孩子吧
- 为什么我的tomcat执行不了jsp,打开是源代码??
- 我怎么什么都不会啊,jsp中有md5加密吗?有的话能给我个程序吗?在线等~~~~~~~
import java.text.SimpleDateFormat;
import java.util.Calendar;public class DateUtil { public static void main(String[] args) {
String s = DateUtil.getDate("2006-3-30", "yyyy-MM-dd");
} /**
* 取出指定时间
*
* @param date Date类型的String参数
* @param pattern 时间模式
* */
public static String getDate(String date, String pattern) {
Calendar c = createCalendar(date, pattern);
if (null == c)
throw new NumberFormatException("format " + date + " error. please input date format.");
//算出给定的时间是星期几
int k = c.get(Calendar.DAY_OF_WEEK) - 2;
long l = c.getTimeInMillis() - 24 * 60 * 60 * 1000 * k;
c.setTimeInMillis(l);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(c.getTime());
}
public static Calendar createCalendar(String date, String pattern) {
if (null == date)
throw new NumberFormatException("format " + date + " error. please input date format.");
try {
SimpleDateFormat f = new SimpleDateFormat(pattern);
Calendar c = Calendar.getInstance();
c.setTime(f.parse(date));
return c;
} catch (ParseException e) {
return null;
}
}
}
Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.你应该用这个 :add(int field, int amount)
Adds the specified (signed) amount of time to the given time field, based on the calendar's rules.