使用正则表达式取时间可以
([0-9]){2,4}[-|/]([0-9]){1,2}[-|/]]([0-9]){1,2}
匹配到了就取出三个子表达式(括号表示的group),得到年月日
正则表达式的API你可以使用jdk1.4的,也可以使用apache的

解决方案 »

  1.   

    String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
     // if no ids were returned, something is wrong. get out.
     if (ids.length == 0)
         System.exit(0);  // begin output
     System.out.println("Current Time"); // create a Pacific Standard Time time zone
     SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]); // set up rules for daylight savings time
     pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
     pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); // create a GregorianCalendar with the Pacific Daylight time zone
     // and the current date and time
     Calendar calendar = new GregorianCalendar(pdt);
     Date trialTime = new Date();
     calendar.setTime(trialTime); // print out a bunch of interesting things
     System.out.println("ERA: "  calendar.get(Calendar.ERA));
     System.out.println("YEAR: "  calendar.get(Calendar.YEAR));
     System.out.println("MONTH: "  calendar.get(Calendar.MONTH));
     System.out.println("WEEK_OF_YEAR: "  calendar.get(Calendar.WEEK_OF_YEAR));
     System.out.println("WEEK_OF_MONTH: "  calendar.get(Calendar.WEEK_OF_MONTH));
     System.out.println("DATE: "  calendar.get(Calendar.DATE));
     System.out.println("DAY_OF_MONTH: "  calendar.get(Calendar.DAY_OF_MONTH));
     System.out.println("DAY_OF_YEAR: "  calendar.get(Calendar.DAY_OF_YEAR));
     System.out.println("DAY_OF_WEEK: "  calendar.get(Calendar.DAY_OF_WEEK));
     System.out.println("DAY_OF_WEEK_IN_MONTH: "
                         calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
     System.out.println("AM_PM: "  calendar.get(Calendar.AM_PM));
     System.out.println("HOUR: "  calendar.get(Calendar.HOUR));
     System.out.println("HOUR_OF_DAY: "  calendar.get(Calendar.HOUR_OF_DAY));
     System.out.println("MINUTE: "  calendar.get(Calendar.MINUTE));
     System.out.println("SECOND: "  calendar.get(Calendar.SECOND));
     System.out.println("MILLISECOND: "  calendar.get(Calendar.MILLISECOND));
     System.out.println("ZONE_OFFSET: "
                         (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
     System.out.println("DST_OFFSET: "
                         (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
     
     System.out.println("Current Time, with hour reset to 3");
     calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
     calendar.set(Calendar.HOUR, 3);
     System.out.println("ERA: "  calendar.get(Calendar.ERA));
     System.out.println("YEAR: "  calendar.get(Calendar.YEAR));
     System.out.println("MONTH: "  calendar.get(Calendar.MONTH));
     System.out.println("WEEK_OF_YEAR: "  calendar.get(Calendar.WEEK_OF_YEAR));
     System.out.println("WEEK_OF_MONTH: "  calendar.get(Calendar.WEEK_OF_MONTH));
     System.out.println("DATE: "  calendar.get(Calendar.DATE));
     System.out.println("DAY_OF_MONTH: "  calendar.get(Calendar.DAY_OF_MONTH));
     System.out.println("DAY_OF_YEAR: "  calendar.get(Calendar.DAY_OF_YEAR));
     System.out.println("DAY_OF_WEEK: "  calendar.get(Calendar.DAY_OF_WEEK));
     System.out.println("DAY_OF_WEEK_IN_MONTH: "
                         calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
     System.out.println("AM_PM: "  calendar.get(Calendar.AM_PM));
     System.out.println("HOUR: "  calendar.get(Calendar.HOUR));
     System.out.println("HOUR_OF_DAY: "  calendar.get(Calendar.HOUR_OF_DAY));
     System.out.println("MINUTE: "  calendar.get(Calendar.MINUTE));
     System.out.println("SECOND: "  calendar.get(Calendar.SECOND));
     System.out.println("MILLISECOND: "  calendar.get(Calendar.MILLISECOND));
     System.out.println("ZONE_OFFSET: "
             (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
     System.out.println("DST_OFFSET: "
             (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
      

  2.   

    import java.awt.Graphics;
    import java.applet.Applet;
    import java.util.Calendar;
    class Time{
        t=Calendar.getInstence();
    y=t.get(t.YEAR);
    m=t.get(t.MONTH)+1;
    d=t.get(t.DATE);
    hh=t.get(t.HOUR_OF_DAY);
    mm=t.get(t.MINUTER);
    ss=t.get(t.SECOND);
    } public String getDate(){
    return y+"年"+m+"月"+d+"日";
     }
     public String getTime(){
    String s=hh+"时"+mm+"分"+ss+"秒";
    return s;
     }
    }  public class ShowTime extends Applet{
    Time t=new Time();
      public void paint(Graphics g){
       g.drawString("当前日期:"+t.getDate(),50,40);
       g.drawString("当前时间:"+t.getTime(),50,80);
       }
     }
      

  3.   

    给你一段经典代码:
    String s1="2000/02/05-09-08";
    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy/MM/dd-HH-mm");
    Date date = simpledateformat.parse(s1);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    System.out.println("month is: "+calendar.get(Calendar.MONTH)+1);
    System.out.println("date is: "+calendar.get(Calendar.HOUR_OF_DAY));
    try一try?
    why not?
      

  4.   

    SimpleDateFormat 找不到,我的字符串是从sql数据库里取出来的,怎么转换呢?我正在一一的试,大家不要着急,分会给的,我再加20分,麻烦各位给我说清楚点好吗,谢谢!
      

  5.   

    select to_char(sysdate,'yyyy-MM-dd hh24:ss') from dual
    然后再处理.SQL语句更好做啊!
      

  6.   

    java.text 
    Class SimpleDateFormat
    java.lang.Object
      |
      +-java.text.Format
            |
            +-java.text.DateFormat
                  |
                  +-java.text.SimpleDateFormat我用的jdk1.3有这个类。
    具体的看文档,讲的很清楚。我基本上也是看文档学的还有哇,如果一个类找不到,可以试着用JWhichimport java.io.PrintStream;
    import java.net.URL;public class JWhich
    {
        public static void which(String s)
        {
            if(!s.startsWith("/"))
                s = "/" + s;
            s = s.replace('.', '/');
            s = s + ".class";
            URL url = (new JWhich()).getClass().getResource(s);
            if(url != null)
                System.out.println("\nClass '" + s + "' found in \n'" + url.getFile() + "'");
            else
                System.out.println("\nClass '" + s + "' not found in \n'" + System.getProperty("java.class.path") + "'");
        }    public static void which(String s, String s1)
        {
            System.setProperty("java.class.path", s1);
            which(s);
        }    public static void main(String args[])
        {
            if(args.length == 1)
                which(args[0]);
            else
            if(args.length == 2)
                which(args[0], args[1]);
            else
                System.err.println("Usage: java JWhich <classname> [classpath]");
        }
    }
      

  7.   

    用java.text.SimpleDateFormat是标准的方法,不过你要把你的字符串格式规定好,特别是小时的格式,是0到23还是1到24,有没有AM/PM等。
      

  8.   

    再请教一下fightboy,我给你加分,但是出现 "贴子回复次数大于跟给分次数 ",什么意思,还有我想多给点分,但是他说给的分数大于40什么的,告诉我怎么给你分