2011-11-07 28 views
1

我正在從包含日期形式爲yyMMdd的文本文件中讀取String然後我想將其轉換爲date類型,但是當我這樣做時,它會丟失它格式。這裏是什麼,我都試過如何將字符串轉換爲日期並保持格式正確

String strDate = matcher.group(10); // strDate would contain 111107 

SimpleDateFormat formatter1 = new SimpleDateFormat("yyMMdd"); 
Date date = formatter1.parse(strDate); // after parsing it, the format changes to Thu Nov 03 00:00:00 EDT 2011 


的一個實例,但如果我參加date並把它放回像這樣String tDate = formatter1.format(date);的字符串的字符串,然後包含Id喜歡看111107格式的日期。

有沒有一種方法,我可以做到這一點?也許如果我能如何呼籲format函數返回date對象而不是String,謝謝。

編輯 我從文件中讀取日期的列表中,並將它們加載到地圖,然後我這些日子比較,這也是在yyMMdd格式,然後如果從地圖上的日期更是對當前日期那麼比當前日期早一週,我提示用戶輸入,然後以yyMMdd格式將日期和其他相關信息寫入文件。我使用地圖的原因是文本文件的每一行包含的一些信息和獨特的名字,我比較了數據的具體線路的日期,所以我做了map.get(aName)

這裏是代碼,希望它有助於

dbConnect(); 
      stmt = conn.createStatement(); 
      String query = "select * from OBJECT_BAC_EV where instance = 12";//VALUE <> 'Normal'"; 
      rslt = stmt.executeQuery(query); 

      Calendar currentDate = Calendar.getInstance(); 
      SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd"); 
      String dateNow = formatter.format(currentDate.getTime()); 
      Date curDate = (Date)formatter.parse(dateNow); 
      currentDate.setTime(curDate); 
      currentDate.add(Calendar.DAY_OF_MONTH, -7); 
      dateNow = formatter.format(currentDate.getTime()); 
      curDate = (Date)formatter.parse(dateNow); 

      Calendar cDate = Calendar.getInstance(); 
      SimpleDateFormat f = new SimpleDateFormat("yyMMdd"); 
      String strDate = f.format(cDate.getTime()); 

      while(rslt.next()) 
      { 
       System.out.println(rslt.getRow()); 

       String aValue = rslt.getString("VALUE"); 
       String aName = rslt.getString("NAME"); 
       String aObjRef = rslt.getString("ObjRef"); 


       if(aNoteMap.containsKey(aName)) 
       { 

        String n = aNoteMap.get(aName); 
        if(aDateMap.get(aName).before(curDate)) 
        { 
         int answer = JOptionPane.showConfirmDialog(null, "Would you like to use last weeks note? " + n, "Hey", JOptionPane.YES_NO_OPTION); 
         if(answer == JOptionPane.YES_OPTION) 
         { 
          output.write(aName + " " + aObjRef + " " + aValue + " " + aDateMap.get(aName) + " " 
            + n + (System.getProperty("line.separator"))); 
         } 
         else if(answer == JOptionPane.NO_OPTION) 
         { 
          String newNote = JOptionPane.showInputDialog("Enter new note"); 
          output.write(aName + " " + aObjRef + " " + aValue + " " + aDateMap.get(aName) + " " 
            + newNote + (System.getProperty("line.separator"))); 
         } 
        } 
        else 
         output.write(aName + " " + aObjRef + " " + aValue + " " + aDateMap.get(aName) + " " 
           + n + (System.getProperty("line.separator"))); 
       } 
       else 
        output.write(aName + " " + aObjRef + " " + aValue + " " + strDate + " " 
          + "" + (System.getProperty("line.separator"))); 
      } 
      System.out.println("its closing output.."); 
      output.close(); 
     } 

回答

3

Date類沒有「內部」格式,它只表示日期元素。要使用特定格式輸出它,您需要按照以下方式進行輸出:String tDate = formatter1.format(date);

您認爲它具有「錯誤格式」的原因可能是因爲當您嘗試輸出它時,默認情況下會執行toString() ,它不會像你想要的那樣格式化它。

如果您向我們提供有關如何使用該日期的更多詳細信息(包括您的代碼),那麼我們可能會提供關於如何將格式化程序注入其中的建議。

+0

我添加了一點更多的描述,以我想要做什麼以及一些代碼 – Beef

2

Date總是存儲包括時間在內的完整信息。如果使用不包含時間信息的SimpleDateFormat解析日期,則這些字段將設置爲0,如您的示例中所示。

Date本身不存儲任何格式信息。

1

寫使用joda-time,它會是這樣的:

String str = "110107"; 
DateTimeFormatter formatter = new DateTimeFormatterBuilder() 
    .appendYearOfCentury(2, 2) 
    .appendMonthOfYear(2) 
    .appendDayOfWeek(2) 
    .toFormatter(); 

DateTime dateFromDB = formatter.parseDateTime(str);//create DateTime instance 
System.out.println("Date:" + formatter.print(dateFromDB));//toString in required format 

而且它有很多其他的好處,顯然它將取代現有的Java日期庫,這一直是相當痛苦的,在即將推出的Java版本。

的好處之一是

DateTime now = new DateTime(); 
Integer compared = now.minusWeeks(1).compareTo(dateFromDB); 

而且做的compareTo的expected

2

的SimpleDateFormat.format方法總是返回一個字符串。它使用指定的格式將'date'參數表示爲String。例如:

SimpleDateFormat sdf = new SimpleDateFormat('yyMMdd'); 
Date date = sdf.parse("110510"); 
String sDate = sdf.format(date); //"110510" 

SimpleDateFormat sdf2 = new SimpleDateFormat('yyyy.MM.dd'); 
String sDate2 = sdf2.format(date); //2011.05.10 

這是格式化日期對象的最佳方法。

1

你可以嘗試這樣的:

class MyDate extends java.util.Date { 
    static final SimpleDateFormat yymmddFormat = new SimpleDateFormat("yyMMdd"); 

    public MyDate (String s) throws ParseException { 
    super(yymmddFormat.parse(s).getTime()); 
    } 

    public String toString() { 
    return yymmddFormat.format(this); 
    } 
} 

使用這個類無處不在,你目前使用的是Date對象。這應該使你的所有日期看起來像你想要的。

相關問題