困绕多时的问题:jsp中获取时间,请各位大侠帮小弟一把~~!

hyc_hover 2006-11-08 05:18:31
在jsp中 如何才能获得当前的时间呢?
获取当前时间之后如何能够获取当前时间之前的时间呢?比如:当前时间的200天以前?
时间格式是 yyyy-MM-dd HH:mm:ss

请各位高手指教啊~~~先谢谢了
...全文
526 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyc_hover 2006-11-09
  • 打赏
  • 举报
回复
Sunny319(努力学习java中.): 谢谢你,问题解决了!

再次谢谢你和其他热心的大侠们!

有这么多热心的朋友,何愁学不好java!
Sunny319 2006-11-09
  • 打赏
  • 举报
回复
不用,大家共同进步!
Sunny319 2006-11-08
  • 打赏
  • 举报
回复
int year_int=2006
int month_int=11
int day_int=8
这个值传到java里面,表示的是20061208.,他的月份是从0开始的.
我疏忽了,不好意思!上面我写了一个完整的代码,而且测试通过!
Sunny319 2006-11-08
  • 打赏
  • 举报
回复
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Date;
class TimeSub
{
public static void main(String[] args)
{
Date now = new Date();
SimpleDateFormat time=new SimpleDateFormat("yyyyMMdd");
System.out.println(time.format(now ));
int year_int=(new java.lang.Integer(time.format(now).substring(0,4))).intValue();
int month_int=(new java.lang.Integer(time.format(now).substring(4,6))).intValue()-1;/*这里月份要减一,因为,java里面0表示一月份*/
int day_int=(new java.lang.Integer(time.format(now).substring(6,8))).intValue();
GregorianCalendar worldTour = new GregorianCalendar(year_int,month_int,day_int);
//worldTour.add(7,-200);/*这里7标识安日偏移,1是年,2是月,负号标识向前偏移200天*/
worldTour.add(7,-1);/*这里7标识安日偏移,1是年,2是月,负号标识向前偏移200天*/
SimpleDateFormat time1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
Date d = worldTour.getTime();
System.out.println(time1.format(d));
}
}
hyc_hover 2006-11-08
  • 打赏
  • 举报
回复
然后把现在时间的年,月,日转化成整数;
int year_int=2006
int month_int=11
int day_int=8

这里还是自己定义的时间呀 能否根据获得的当前时间自动计算出前200天的时间?
而且 worldTour.add(7,-200);这里也有错误 如果用 worldTour.add(7,-1);的话将不是得到 11.7号
Camelh 2006-11-08
  • 打赏
  • 举报
回复
赞同楼上Sunny319(努力学习java中.) 的;实际上就用GregorianCalendar,然后加-200就可以了
Sunny319 2006-11-08
  • 打赏
  • 举报
回复
取得当前时间:
Date now = new Date();
SimpleDateFormat time=new SimpleDateFormat("yyyyMMdd");
System.out.println(time.format(now ));
然后把现在时间的年,月,日转化成整数;
int year_int=2006
int month_int=11
int day_int=8
GregorianCalendar worldTour = new GregorianCalendar(year_int,month_int,day_int);
worldTour.add(7,-200);/*这里7标识安日偏移,1是年,2是月,负号标识向前偏移200天*/
SimpleDateFormat time1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
Date d = worldTour.getTime();
System.out.println(time1.format(d););
atttongmu 2006-11-08
  • 打赏
  • 举报
回复
当前时间:
public static Date getNowDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString=formatter.format(currentTime);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}
怡静如诗 2006-11-08
  • 打赏
  • 举报
回复
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR,200);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = sdf.format(cal.getTime());

jackxing 2006-11-08
  • 打赏
  • 举报
回复
查下Calendar类

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧