2016-03-03 108 views
0

我正在使用joda時間來處理日期時間。我用這讓手機的日期格式:使用當前電話格式替換月份名稱的月份編號

SimpleDateFormat dateFormat = (SimpleDateFormat) DateFormat.getDateFormat(context); 
String datePattern = dateFormat.toPattern(); 

然後我用此格式的日期時間爲String:

DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(datePattern); 
dateFormatter.print(dateTime) 

例如,它會顯示日期時間的字符串:

3/1/2016

但我想要它顯示:

月/2016分之1

月/2016分之1

我怎樣才能做到這一點?

+0

如果你想要一個特定的格式,使用它。但只是用月份名稱替換月份數字可能會在某些格式中變得非常奇怪。聽起來對我來說是個壞主意。 –

+0

@JonSkeet你是對的,也許我會考慮這個 –

回答

-1

嘗試......

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); 
Date date_1 = sdf1.parse("2016-03-08"); 
SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MMM/yyyy"); 
System.out.println(sdf2.format(date_1)); 
+0

這忽略了「使用當前手機格式」部分的問題。 –

+0

@JonSkeet,檢查編輯的答案,因爲這次使用當前的電話格式 –

+0

不,它不是。您仍然對格式進行硬編碼......它不會因用戶區域設置而異。 –

0

您可以使用下面的方法

public static String getDateFormattedString(String sourceFormat,String dateString,String targetFormat) { 
    SimpleDateFormat mOriginalFormat = new SimpleDateFormat(sourceFormat);//3/1/2016 
    SimpleDateFormat mTargetFormat = new SimpleDateFormat(targetFormat);//Mar/1/2016 

    String reqstring = null; 
    try { 
     reqstring = mTargetFormat.format(mOriginalFormat.parse(dateString)); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    return reqstring; 
} 

,然後調用

String resultantDate=getDateFormattedString("MM/dd/yyyy", "3/1/2016", "MMM/d/yyyy"); 

使用目標格式:MMM/d/yyyy for Mar/1/2016MMMMM/d/yyyy forar March/1/2016

+0

我認爲問題在於OP正試圖獲得「MMM/d/yyyy」模式 - 沒有跡象表明他們需要解析並重新格式化... –

+0

感謝Jon。以下代碼有助於。 String mFormat = datePattern.substring(datePattern.indexOf('M'),datePattern.lastIndexOf('M')+ 1); (MMFormat,「MMM」); //「MMMM」 \t \t DateTimeFormatter dateFormatter = DateTimeFormat。forPattern(datePattern); \t \t dateFormatter.print(dateTime) –

+0

儘管如此,您的代碼示例仍然會解析並重新格式化。我建議你編輯答案 - 這仍然不是我想要的,說實話...我從根本上不認爲這是一個好主意。 –

0

您必須編輯圖案才能達到所需的效果。建議:

java.util.Date d = new Date(); 
SimpleDateFormat dateFormat = (SimpleDateFormat) DateFormat.getDateFormat(context); 
String pattern = dateFormat.toPattern(); 

DateTime joda = new DateTime(d); // in default timezone 
DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern).withLocale(Locale.ENGLISH); 
System.out.println("old format: " + fmt.print(joda)); 

// does not handle the case where the month appears twice or more in pattern (simplification) 
int count = 0; 
for (int i = 0; i < pattern.length(); i++) { 
    if (pattern.charAt(i) == 'M') { 
     count++; 
    } 
} 

// your first configuration parameter 
boolean wantsAbbreviation = false; 

// your second configuration parameter - consider Locale.getDefault() 
Locale locale = Locale.ENGLISH; 

String replacement = (wantsAbbreviation ? "MMM" : "MMMM"); 

if (count == 2) { 
    pattern = pattern.replace("MM", replacement); 
} else if (count == 1) { 
    pattern = pattern.replace("M", replacement); 
} else { 
    // either no month or already text format 
} 

DateTimeFormatter fmtNew = DateTimeFormat.forPattern(pattern).withLocale(locale); 
System.out.println("new format: " + fmtNew.print(joda)); 

// old format: 03/03/2016 
// new format: March/03/2016 

不過,我不給,該手機格式總是使用由喬達時間瞭解,由於SimpleDateFormat和喬達 - 時間格式模式的定義是不相同的圖案字母任何保證。但至少對於月份模式符號「M」,它應該起作用。最後,你還要檢查字母「L」(獨立格式的月份,與許多奴隸語言相關),但該信不被Joda-Time理解。