想得到当前系统的日期,要求是字符格式(YYYYMMDD),必须是八位,也就是20060916的格式
再得当前系统的时间,要求是字符格式(HH:MM:SS)

解决方案 »

  1.   

    Date currentDate = Calendar.getInstance().getTime();
    String formatDate = 
                 SimpleDateFormat("yyyyMMdd hh:mm:ss")).format(currentDate);
      

  2.   

    不对啊,出错啊
    我是在JSP页面里写的
    org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 7 in the jsp file: /insert.jsp
    Generated servlet error:
    The type Date is ambiguousAn error occurred at line: 7 in the jsp file: /insert.jsp
    Generated servlet error:
    The method SimpleDateFormat(String) is undefined for the type insert_jsp
      

  3.   

    import java.util.*;
    class DateTime
    {
    public static void main(String[] args)
        {
         Calendar c=Calendar.getInstance();
         int year=c.get(Calendar.YEAR);
         int month=c.get(Calendar.MONTH)+1;
         int date=c.get(Calendar.DATE);
         String str1=year+(month<10?"0"+month:""+month)+(date<10?"0"+date:""+date);
         int hour=c.get(Calendar.HOUR);
         int minute=c.get(Calendar.MINUTE);
         int second=c.get(Calendar.SECOND);
         if(c.get(Calendar.AM_PM)==Calendar.PM)
         hour+=12;
         String str2=(hour<10?"0"+hour:""+hour)+(minute<10?":0"+minute:":"+minute)+(second<10?":0"+second:":"+second);
         System.out.println (str1);
         System.out.println (str2);
        }
    }
      

  4.   

    晕呀,太复杂点了吧,就没有一个函数,直接转的吗?
    比如PB中的:string(now(),"HH:MM:SS"),string(today(),"YYYYMMDD"),就没有这样的东西吗?
      

  5.   

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    Date d = new Date();
    String strDate = sdf.format(d);
    System.out.println(strDate);
    }
      

  6.   

    我也学到了怎么用SimpleDateFormat来格式化时间,再也不会用笨方法了,HOHO~~