求指定日期的n小时后的日期后怎么计算?
解决方案 »
- 求一个struts2+spring+hibernate项目源码,学习用
- 哪位仁兄有JSF2.0的教程啊,英文的也行,谢谢啊
- java string(带小数的) 类型 转换成 秒的格式。。
- LifeCycleServlet.java:11: 找不到符号
- 反射模仿struts1中action继承DispatchAction
- 各位朋友,这是我的开发总结,想在北京找份工作,不知道何种岗位比较适合,请帮忙推荐一下,谢谢!!
- Applet如何添加事件响应
- org.hibernate.exception.SQLGrammarException: could not execute query错误,请高手解决一下
- 关于JVM出错的问题?大家来帮忙看看!来者有分!!!
- xml文档有了,weblogic下面怎么用jsp调用?请给一段程序。
- java学习感觉到了瓶颈??暑假到了,求高人指点学习方向。
- 在线计数器
cal.setTime(new Date());
cal.add(cal.MONTH, 3);
进行日期和小时增删
c.setTimeInMillis(getMillis(date) + n * 3600 * 1000);
方法二 同楼上calendar.add(Calendar.HOUR, 1);
public class AddHours { /**
* 获得指定日期的N小时后日期
* @return String 类型日期
*/
public static String getHoursLater(Date date,int hours){
String hoursLater="";
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, hours);
hoursLater=dateToString(cal.getTime(),"yyyy-MM-dd HH:mm:dd");
return hoursLater;
}
/**
* 将日期按指定格式转换成字符串
* @param date
* @param style
* @return
*/
public static String dateToString(Date date, String style) {
SimpleDateFormat sdf = new SimpleDateFormat(style);
if (date != null) {
return sdf.format(date);
}
return null;
}
/**
* Main
* @param args
*/
public static void main(String[] args) {
System.out.println(dateToString(new Date(),"yyyy-MM-dd HH:mm:dd"));
System.out.println(getHoursLater(new Date(),2));
}
}