站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题困绕多时的问题:jsp中获取时间,请各位大侠帮小弟一把~~!

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

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


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

请各位高手指教啊~~~先谢谢了


查下Calendar类


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());




当前时间:
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;
}


取得当前时间:
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););


赞同楼上Sunny319(努力学习java中.) 的;实际上就用GregorianCalendar,然后加-200就可以了


然后把现在时间的年,月,日转化成整数;
int year_int=2006
int month_int=11
int day_int=8

这里还是自己定义的时间呀 能否根据获得的当前时间自动计算出前200天的时间?
而且 worldTour.add(7,-200);这里也有错误 如果用 worldTour.add(7,-1);的话将不是得到 11.7号


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));
}
}


int year_int=2006
int month_int=11
int day_int=8
这个值传到java里面,表示的是20061208.,他的月份是从0开始的.
我疏忽了,不好意思!上面我写了一个完整的代码,而且测试通过!


Sunny319(努力学习java中.): 谢谢你,问题解决了!

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

有这么多热心的朋友,何愁学不好java!

不用,大家共同进步!


↑返回目录
前一篇: 关于 javamail 收取126邮箱的问题....
后一篇: JSP连接MSSQL数据库的时候,TOMCAT里出现这个错误,请帮忙下!