<%= new java.util.Date() %>结果可能有点区别。 Fri Nov 19 09:02:52 CST 2004

解决方案 »

  1.   

    是啊,这个不行。我是想做客户端显示当前时间,为了统一,我取的是服务器端时间。然后用javascript做的+1操作。而JSP与javascript的格式不一致。
      

  2.   

    import java.util.*,java.text.*
    DateFormat format=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)
    //上面两个参数是日期和时间格式,或你看一下JDK文档的详细介绍
    试试吧
      

  3.   

    换一个方式想想!Date currentTime=new Date();
    String y=""+(currentTime.getYear()+1900);
    String m=""+(currentTime.getMonth()+1);
    String d=""+(currentTime.getDay());
    if(m.length()==1){m="0"+m;}
    if(d.length()==1){d="0"+d;}
    --------------------------------------
    或者试试
    SimpleDateFormat formatter= new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
    Date currentTime=new Date();
    out.print(formatter.format(currentTime));
      

  4.   

    java里面好象不支持这样的格式
      

  5.   

    那有什么方法可以在jsp里实现当前时间的显示呢?(这个要走动的),而且要客户端统一。
      

  6.   

    to:fruitfull(fruitfull)你那种是在客户端显示,不能时时更新啊?
      

  7.   

    时间肯定是由你的服务器发送过去才能统一嘛!
    如果要动态地更新那只能用applet
    这样的例子很多的,不是有一个很经典的用多线程实现的钟吗?
    楼主搜一下
      

  8.   

    把时间传给javascriptfunction showtime( h,m,s)
    {
    document.write(h+":"+m+":"+s);
    s=s+1;
    if(s==60){s=0;m=m+1;}
    if(m==60){m=0;h=h+1;}
    setTimeout("showtime(h,m,s)",1000);
    }
      

  9.   

    to:fruitfull(fruitfull)你的方法测试通过了吗?我试怎么不行呢?也不走啊?
      

  10.   

    long dateInMilliSeconds = System.currentTimeMillis();
    Date date = new Date(dateInMilliSeconds);
    System.out.println(date);
      

  11.   

    从你给的时间看,Sun Dec 19 08:00:06 UTC+0800 2004UTC表示校准的全球时间你所在的时区应该是 +08:00  中国沿海时间 转化成UTC格式,应该正确设置时区,以免得到错误的时间。 public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zZ yyyy",Locale.ENGLISH);
    // 设置时区
    sdf.setTimeZone(new SimpleTimeZone(28800000,"UTC"));

    // 当前时间,也就是服务器时间
    Date serverDate = new java.util.Date();
    String strServerDate = sdf.format(new java.util.Date());
    System.out.println("服务器时间:" + strServerDate);

    // 创建Calendar
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(serverDate);

    // 服务器时间加1秒
    calendar.add(Calendar.MILLISECOND, 1000);
    Date clientDate = calendar.getTime();
    String strClientDate = sdf.format(clientDate);
    System.out.println("客户端时间:" + strClientDate);

    // 从已知时间解析后加1秒
    String strADate = "Sun Dec 19 08:00:06 UTC+0800 2004";
    java.util.Date aDate = sdf.parse(strADate);
    calendar.setTime(aDate);
    calendar.add(Calendar.MILLISECOND,1000);
    Date bDate = calendar.getTime();
    String strBDate = sdf.format(bDate);

    // print 
    System.out.println("解析前:" + strADate);
    System.out.println("加一秒:" + strBDate);


    }
      

  12.   

    运行结果如下:服务器时间:Fri Nov 19 15:47:43 UTC+0800 2004
    客户端时间:Fri Nov 19 15:47:44 UTC+0800 2004
    解析前:Sun Dec 19 08:00:06 UTC+0800 2004
    加一秒:Sun Dec 19 08:00:07 UTC+0800 2004