String str = "2002/4/22";
str = str.replace('/','');

解决方案 »

  1.   

    刚刚查了一下,没有适合要求的。我的目的是返回当然日期格式为YYYYMMRR(例如20020422)的字符串
      

  2.   

    进一步解释:我现在写了一个日期类MyDate,class MyDate {
      private int year;
      private int month;
      private int day;  ....  public String printYearMonthDay() {
           在此需要用 year,month, day,来构造YYYYMMDD格式的字符串(如:20020501)
      }请问有没有现成的包?具体那个包那个方法?谢谢!
      

  3.   

    去看看FORMAT这个类
    里面有很多东西
    java.text.*
      

  4.   

    /**
      * 根据pattern指定的样式转换格式的日期
      * @param date    要转换的日期
      * @param pattern 要指定的得日期格式
      * @return String
      * @throws IllegalArgumentException 当两个参数出错时抛出此异常<br>
      *
      * 下为日期格式的简单描述详情请参看javaApi中java.text.SimpleDateFormat<br>
      * The following pattern letters are defined (all other characters from
      * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
      * <code>'z'</code> are reserved):
      * <blockquote>
      * <table border=0 cellspacing=3 cellpadding=0>
      *     <tr bgcolor="#ccccff">
      *         <th align=left>Letter
      *         <th align=left>Date or Time Component
      *         <th align=left>Presentation
      *         <th align=left>Examples
      *     <tr>
      *         <td><code>G</code>
      *         <td>Era designator
      *         <td><a href="#text">Text</a>
      *         <td><code>AD</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>y</code>
      *         <td>Year
      *         <td><a href="#year">Year</a>
      *         <td><code>1996</code>; <code>96</code>
      *     <tr>
      *         <td><code>M</code>
      *         <td>Month in year
      *         <td><a href="#month">Month</a>
      *         <td><code>July</code>; <code>Jul</code>; <code>07</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>w</code>
      *         <td>Week in year
      *         <td><a href="#number">Number</a>
      *         <td><code>27</code>
      *     <tr>
      *         <td><code>W</code>
      *         <td>Week in month
      *         <td><a href="#number">Number</a>
      *         <td><code>2</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>D</code>
      *         <td>Day in year
      *         <td><a href="#number">Number</a>
      *         <td><code>189</code>
      *     <tr>
      *         <td><code>d</code>
      *         <td>Day in month
      *         <td><a href="#number">Number</a>
      *         <td><code>10</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>F</code>
      *         <td>Day of week in month
      *         <td><a href="#number">Number</a>
      *         <td><code>2</code>
      *     <tr>
      *         <td><code>E</code>
      *         <td>Day in week
      *         <td><a href="#text">Text</a>
      *         <td><code>Tuesday</code>; <code>Tue</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>a</code>
      *         <td>Am/pm er
      *         <td><a href="#text">Text</a>
      *         <td><code>PM</code>
      *     <tr>
      *         <td><code>H</code>
      *         <td>Hour in day (0-23)
      *         <td><a href="#number">Number</a>
      *         <td><code>0</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>k</code>
      *         <td>Hour in day (1-24)
      *         <td><a href="#number">Number</a>
      *         <td><code>24</code>
      *     <tr>
      *         <td><code>K</code>
      *         <td>Hour in am/pm (0-11)
      *         <td><a href="#number">Number</a>
      *         <td><code>0</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>h</code>
      *         <td>Hour in am/pm (1-12)
      *         <td><a href="#number">Number</a>
      *         <td><code>12</code>
      *     <tr>
      *         <td><code>m</code>
      *         <td>Minute in hour
      *         <td><a href="#number">Number</a>
      *         <td><code>30</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>s</code>
      *         <td>Second in minute
      *         <td><a href="#number">Number</a>
      *         <td><code>55</code>
      *     <tr>
      *         <td><code>S</code>
      *         <td>Millisecond
      *         <td><a href="#number">Number</a>
      *         <td><code>978</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>z</code>
      *         <td>Time zone
      *         <td><a href="#timezone">General time zone</a>
      *         <td><code>Pacific Standard Time</code>; <code>PST</code>; <code>GMT-08:00</code>
      *     <tr>
      *         <td><code>Z</code>
      *         <td>Time zone
      *         <td><a href="#rfc822timezone">RFC 822 time zone</a>
      *         <td><code>-0800</code>
      * </table>
      * </blockquote>
      *<h4>Examples</h4>
      *
      * The following examples show how date and time patterns are interpreted in
      * the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time
      * in the U.S. Pacific Time time zone.
      * <blockquote>
      * <table border=0 cellspacing=3 cellpadding=0>
      *     <tr bgcolor="#ccccff">
      *         <th align=left>Date and Time Pattern
      *         <th align=left>Result
      *     <tr>
      *         <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code>
      *         <td><code>2001.07.04 AD at 12:08:56 PDT</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>"EEE, MMM d, ''yy"</code>
      *         <td><code>Wed, Jul 4, '01</code>
      *     <tr>
      *         <td><code>"h:mm a"</code>
      *         <td><code>12:08 PM</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>"hh 'o''clock' a, zzzz"</code>
      *         <td><code>12 o'clock PM, Pacific Daylight Time</code>
      *     <tr>
      *         <td><code>"K:mm a, z"</code>
      *         <td><code>0:08 PM, PDT</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
      *         <td><code>02001.July.04 AD 12:08 PM</code>
      *     <tr>
      *         <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
      *         <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>
      *     <tr bgcolor="#eeeeff">
      *         <td><code>"yyMMddHHmmssZ"</code>
      *         <td><code>010704120856-0700</code>
      * </table>
      * </blockquote>
      */
     public static String trimFormat(Date date,String pattern) throws IllegalArgumentException {
         if(date==null)
               throw new IllegalArgumentException("date");
         if(pattern==null)
               throw new IllegalArgumentException("format");
         SimpleDateFormat objSDateFormat= new SimpleDateFormat(pattern);
         return objSDateFormat.format(date);
     }
    }
      

  5.   

    import java.text.DateFormat;
    import java.text.*;....
    java.util.Date dtCallTime = new java.util.Date();
    SimpleDateFormat simDate =  new SimpleDateFormat ("yyyyMMddHHmmss");
    String strReturn = simDate.Format(dtCallTime);
      

  6.   

    我在用时间排序的时候也遇见这个问题了,自己编了一个
    <%
       String today=new String();
    {
       String da=(new java.util.Date()).toString();
       String[] month={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
       String tomonth=da.substring(4,7);  //month
       today=da.substring(24,28)+"-";    //store the year and separater
       for(int k=0;k<12;k++)
       {
           if(tomonth.equals(month[k]))
           {
               if(k<9)
                   today+="0";             //to standard month form
               today+=String.valueOf(k+1); //store the month
               today+="-";
               break;
           }
       }
       today+=da.substring(8,11);          //store the day with space at the end
       today+=da.substring(11,19);          //store the time
    }
    %>
      

  7.   

    上面这位跟我的想发一致,现在关键就在自己构造了时间类型,没用系统的,所以SimpleDateFormat 用不上。不过谢谢这么多人,热心讨论!