簡體   English   中英

Java日期格式轉換 - 月份出錯

[英]Java date format conversion - getting wrong month

我在轉換 Java 中的日期時遇到問題,不知道我哪里出錯了...

    String dateStr = "2011-12-15";
    String fromFormat = "yyyy-mm-dd";
    String toFormat = "dd MMMM yyyy";

    try {
        DateFormat fromFormatter = new SimpleDateFormat(fromFormat);
        Date date = (Date) fromFormatter.parse(dateStr);

        DateFormat toformatter = new SimpleDateFormat(toFormat);
        String result = toformatter.format(date);

    } catch (ParseException e) {
        e.printStackTrace();
    }

輸入日期是 2011-12-15,我期望結果是“2011 年 12 月 15 日”,但我得到的結果是“2011 年 1 月 15 日”

我哪里錯了?

您的fromFormat使用分鍾,而它應該使用幾個月。

String fromFormat = "yyyy-MM-dd";

我認為fromFormat應該是“yyyy-MM-dd”。

這是格式:

  • m == 以小時為單位的分鍾
  • M == 年中的月份

更多: http : //docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

查看SimpleDateFormat的 javadoc 並查看m代表什么。 不是你想象的幾個月,而是幾分鍾。

 String fromFormat = "yyyy-MM-dd"; 

從格式應該是:

String fromFormat = "yyyy-MM-dd"

tl;博士

LocalDate.parse( "2011-12-15" )                            // Date-only, without time-of-day, without time zone.
.format(                                                   // Generate `String` representing value of this `LocalDate`. 
    DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG )  // How long or abbreviated?
                     .withLocale(                          // Locale used in localizing the string being generated.
                         new Locale( "en" , "IN" )         // English language, India cultural norms.
                     )                                     // Returns a `DateTimeFormatter` object.
)                                                          // Returns a `String` object.

2011 年 12 月 15 日

時間

雖然接受的答案是正確的(大寫MM表示月份),但現在有更好的方法。 麻煩的舊日期時間類現在是遺留的,被 java.time 類取代。

您的輸入字符串采用標准ISO 8601格式。 所以不需要為解析指定格式模式。

LocalDate ld = LocalDate.parse( "2011-12-15" );  // Parses standard ISO 8601 format by default.
Locale l = new Locale( "en" , "IN" ) ;  // English in India.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG )
                                       .withLocale( l );
String output = ld.format( f );

轉儲到控制台。

System.out.println( "ld.toString(): " + ld );
System.out.println( "output: " + output );

ld.toString(): 2011-12-15

產出:2011年12月15日

在 IdeOne.com 中查看實時代碼


關於java.time

java.time框架內置於 Java 8 及更高版本中。 這些類取代麻煩的老傳統日期時間類,如java.util.DateCalendar ,和SimpleDateFormat

現在處於維護模式Joda-Time項目建議遷移到java.time類。

要了解更多信息,請參閱Oracle 教程 並在 Stack Overflow 上搜索許多示例和解釋。 規范是JSR 310

您可以直接與您的數據庫交換java.time對象。 使用符合JDBC 4.2或更高版本的JDBC 驅動程序 不需要字符串,不需要java.sql.*類。

從哪里獲得 java.time 類?

ThreeTen-Extra項目用額外的類擴展了 java.time。 該項目是未來可能添加到 java.time 的試驗場。 您可以在這里找到一些有用的類,比如IntervalYearWeekYearQuarter ,和更多

SimpleDateFormat m代表分鍾,而M代表月份。 因此,您的第一個格式應該是yyyy-MM-dd

那么這可能不是你的情況,但可能會幫助某人。 在我的情況下,轉換后,月份的日期和月份設置為 1。所以無論日期是什么,轉換后我得到 1 jan,這是錯誤的。 經過努力,我發現在日期格式中我使用了YYYY而不是yyyy 當我將所有大寫 Y 更改為 y 時,它工作正常。

暫無
暫無

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

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