在jsp中 如何才能获得当前的时间呢?
获取当前时间之后如何能够获取当前时间之前的时间呢?比如:当前时间的200天以前?
时间格式是 yyyy-MM-dd HH:mm:ss 请各位高手指教啊~~~先谢谢了

解决方案 »

  1.   

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

  2.   

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

  3.   

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

  4.   

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

  5.   

    然后把现在时间的年,月,日转化成整数;
     int year_int=2006
     int month_int=11
     int day_int=8这里还是自己定义的时间呀 能否根据获得的当前时间自动计算出前200天的时间?
    而且 worldTour.add(7,-200);这里也有错误 如果用 worldTour.add(7,-1);的话将不是得到 11.7号
      

  6.   

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

  7.   

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

  8.   

    Sunny319(努力学习java中.):  谢谢你,问题解决了! 再次谢谢你和其他热心的大侠们!有这么多热心的朋友,何愁学不好java!