簡體   English   中英

日期格式轉換為21世紀日期

[英]Date Format conversion to 21st century dates

我有DD/MM/YY格式的日期(類似這樣的日期: 10/12/03 )。 我想使用SimpleDateFormat將其格式化為其他格式(即MM/dd/yyyy )。

當我嘗試這個:

try {
                // Old Date is 10/12/03
                newDate = simpleDateFormat.format(format.parse(oldDate));
            } catch (ParseException e) {
                e.printStacktrace();
            }

目前,格式化后,年份0003但我希望年份為2003 請問對此有何投入?

看看SimpleDateFormat#set2DigitYearStart

這使您可以定義兩位數年份的“基准”年份,例如...

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2000);

SimpleDateFormat inFormat = new SimpleDateFormat("dd/MM/yy");
inFormat.set2DigitYearStart(cal.getTime());
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy");
try {
    // Old Date is 10/12/03
    String value = outFormat.format(inFormat.parse("10/12/03"));
    System.out.println(value);
} catch (ParseException e) {
    e.printStackTrace();
}

將輸出12/10/2003

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM