2013-08-01 80 views
0

在我的代碼中,我使用日期格式爲dd/mm/yyyy的用戶輸入,並將結果顯示爲字符串。例如,如果我輸入2011年2月2日,它應該顯示爲'2011年2月2日'。但是我的代碼總是將JAN顯示爲月份。Java中的ConvertDate

import java.util.*; 
import java.text.*; 
public class ConvertDate { 
public static void main(String[] args) { 


Scanner sc = new Scanner(System.in); 
System.out.print("Enter Date: "); 
String ind = sc.nextLine(); 

DateFormat df = new SimpleDateFormat("dd/mm/yyyy"); 
Date d = null; 
try { 

d=df.parse(ind); 

} 
catch(ParseException e) { 
System.out.println("Unable to parse " + ind);} 


DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG); 


String s3 = df3.format(d); 


System.out.println("The entered date is: " + s3); 

} 
} 
+1

http://stackoverflow.com/questions/4966652/java-date-problem-in-parsing?rq=1 – Tala

+0

看到這個[http://stackoverflow.com/questions/12986863/java-calendar日期錯誤] [1] [1]:http://stackoverflow.com/questions/12986863/java-calendar-date-error – Lai

+0

嘗試Joda DateTime庫。 – OrangeDog

回答

4

我覺得它的東西更像是:

DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 

m是分的,M爲一個月。

+0

Nizil它將再次以輸入格式顯示日期..我想顯示mnth像二月FEB ...如此 –

+0

Thannx NIZIL偉大 –

2

正確的日期格式應該如下:

final DateFormat inputDf= new SimpleDateFormat("dd/MM/yyyy"); 
final DateFormat outputDf= new SimpleDateFormat("dd MMM yyyy"); 

它將如下給出輸出:

Input: 01/08/2013 
Output: 01 Aug 2013 
0

格式字符串,m是分鐘,M是一個月。

0

如果您期待「二月」,但看到「一月」,這可能是由於日曆的索引。 見similar question