2011-01-28 185 views
0

我有格式字符串01-Jan-11,我需要將它解析爲格式爲01-Jan-11的日期。 問題是,當我試圖做到這一點時,我總是把一些東西放在下面。 Mon Jan 01 00:00:00 GMT + 05:30 2011將字符串轉換爲日期

有人能幫我做到嗎?

嘗試{

String str_date="11-Jan-11"; 
DateFormat formatter ; 
Date date ; 
     formatter = new SimpleDateFormat("dd-MMM-yy"); 
      date = (Date)formatter.parse(str_date);  
      System.out.println("Today is " +date); 
} catch (ParseException e) 

{System.out.println("Exception :"+e); }  
    } 

時區是GMT + 05:30 Kalkata

+0

發佈您的代碼,和你的時區) – 2011-01-28 15:54:28

+1

而你的問題是?你正在解析2011年1月1日,並得到2011年1月1日。你在期待什麼? – jarnbjo 2011-01-28 16:00:59

回答

2

你是在正確的parse一個字符串轉換爲日期。你的問題,你正在把日期,並將其發送到println,其實質上稱爲toString,其中包含所有你不想要的其他東西(秒,GMT偏移等)。由於您已經配置了格式化程序以滿足您的需求,因此只需使用其format方法:

System.out.println("Today is " + formatter.format(date));