SimpleDateFormat 解析时忽略月份

发布于 2024-09-05 22:40:55 字数 333 浏览 8 评论 0原文

以下代码给出的解析日期为“Wed Jan 13 00:00:00 EST 2010” 而不是“2010 年 6 月 13 日星期三 00:00:00 EST 2010”。任何想法都非常感激。

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss");

    String str = "2010-06-13T00:00:00";

    Date date = sf.parse(str);

    System.out.println(" Date " + date.toString());

The following code is giving me the parsed date as "Wed Jan 13 00:00:00 EST 2010"
instead of "Wed Jun 13 00:00:00 EST 2010". Any ideas much appreciated.

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss");

    String str = "2010-06-13T00:00:00";

    Date date = sf.parse(str);

    System.out.println(" Date " + date.toString());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

漫漫岁月 2024-09-12 22:40:55

尝试:

"yyyy-MM-dd'T'HH:mm:ss"

MM 表示月份。 mm 表示分钟。请参阅 SimpleDateFormat< 的文档/a> 了解支持的日期和时间模式的更多详细信息。

Try:

"yyyy-MM-dd'T'HH:mm:ss"

MM means month. mm means minutes. See the documentation for SimpleDateFormat for more details of the supported date and time patterns.

夜清冷一曲。 2024-09-12 22:40:55

问题是您使用“mm”作为月份,“mm”代表分钟。
以下是所有可用的日期格式,请在此处阅读更多文档。

Symbol  Meaning                Kind         Example
D       day in year             Number        189
E       day of week             Text          E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T
F       day of week in month    Number        2 (2nd Wed in July)
G       era designator          Text          AD
H       hour in day (0-23)      Number        0
K       hour in am/pm (0-11)    Number        0
L       stand-alone month       Text          L:1 LL:01 LLL:Jan LLLL:January LLLLL:J
M       month in year           Text          M:1 MM:01 MMM:Jan MMMM:January MMMMM:J
S       fractional seconds      Number        978
W       week in month           Number        2
Z       time zone (RFC 822)     Time Zone     Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00
a       am/pm marker            Text          PM
c       stand-alone day of week Text          c/cc/ccc:Tue, cccc:Tuesday, ccccc:T
d       day in month            Number        10
h       hour in am/pm (1-12)    Number        12
k       hour in day (1-24)      Number        24
m       minute in hour          Number        30
s       second in minute        Number        55
w       week in year            Number        27
G       era designator          Text          AD
y       year                    Number        yy:10 y/yyy/yyyy:2010
z       time zone               Time Zone     z/zz/zzz:PST zzzz:Pacific Standard 

The problem is that you're using 'mm' as month and 'mm' represents minutes.
Below is all date formats available, read more doc here.

Symbol  Meaning                Kind         Example
D       day in year             Number        189
E       day of week             Text          E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T
F       day of week in month    Number        2 (2nd Wed in July)
G       era designator          Text          AD
H       hour in day (0-23)      Number        0
K       hour in am/pm (0-11)    Number        0
L       stand-alone month       Text          L:1 LL:01 LLL:Jan LLLL:January LLLLL:J
M       month in year           Text          M:1 MM:01 MMM:Jan MMMM:January MMMMM:J
S       fractional seconds      Number        978
W       week in month           Number        2
Z       time zone (RFC 822)     Time Zone     Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00
a       am/pm marker            Text          PM
c       stand-alone day of week Text          c/cc/ccc:Tue, cccc:Tuesday, ccccc:T
d       day in month            Number        10
h       hour in am/pm (1-12)    Number        12
k       hour in day (1-24)      Number        24
m       minute in hour          Number        30
s       second in minute        Number        55
w       week in year            Number        27
G       era designator          Text          AD
y       year                    Number        yy:10 y/yyy/yyyy:2010
z       time zone               Time Zone     z/zz/zzz:PST zzzz:Pacific Standard 
孤蝉 2024-09-12 22:40:55

现代答案:

    String str = "2010-06-13T00:00:00";
    LocalDateTime dateTime = LocalDateTime.parse(str);
    System.out.println("Date-time " + dateTime);

输出:

日期时间 2010-06-13T00:00

我正在使用并推荐 java.time,现代 Java 日期和时间 API。我们甚至不需要显式的格式化程序来进行解析。这是因为您的字符串采用 ISO 8601 格式,这是 java.time 类默认解析的国际标准。 java.time 于 2014 年问世。

虽然在 2010 年提出这个问题时,SimpleDateFormat 是我们用于解析日期和时间的类,但该类现在被认为早已过时了,幸运的是,因为这也很麻烦。

如果您的字符串仅包含日期而没有一天中的时间,请以完全相同的方式使用LocalDate类(这是在重复的问题)。

    String dateStr = "2018-05-23";
    LocalDate date2 = LocalDate.parse(dateStr);
    System.out.println(date2);

2018-05-23

链接: Oracle 教程:日期时间 解释如何使用java.time

Modern answer:

    String str = "2010-06-13T00:00:00";
    LocalDateTime dateTime = LocalDateTime.parse(str);
    System.out.println("Date-time " + dateTime);

Output:

Date-time 2010-06-13T00:00

I am using and recommending java.time, the modern Java date and time API. We don’t even need an explicit formatter for parsing. This is because your string is in ISO 8601 format, the international standard that the java.time classes parse as their default. java.time came out in 2014.

While in 2010 when this question was asked, SimpleDateFormat was what we had for parsing dates and times, that class is now considered long outdated, fortunately, because it was also troublesome.

In case your string contained only a date without time of day, use the LocalDate class in quite the same manner (this was asked in a duplicate question).

    String dateStr = "2018-05-23";
    LocalDate date2 = LocalDate.parse(dateStr);
    System.out.println(date2);

2018-05-23

Link: Oracle tutorial: Date Time explaining how to use java.time.

佞臣 2024-09-12 22:40:55

例如,如果日期是 06 07 2016,

SimpleDateFormat sdf= new SimpleDateFormat("dd MM yyyy");

您可以在这些格式之间使用逗号、句号、斜杠或连字符。

 Ex: dd-mm-yyyy, it will display like(06-07-2016)
 dd/mm/yyyy,it will display like(06/07/2016)
 dd.mm.yyyy,it will display like(06.07.2016)
 dd,mm,yyyy ,it will display like(06,07,2016)

 MM   - will display number of the Month.
 MMM  - will display Month Three character only(Ex: Jul)
 MMMM - will display full month(Ex: July)

 yyyy - will display full year(2016) 
 yy   - will display last two digits(16)

 hh - will display hours
 mm -will display minutes
 ss - will display seconds
 a - will display AM or PM

 Ex: if time is 12:09:10 PM means (hh:mm:ss a)

 EEE- will display short week name(Ex: Wed)
 EEEE- will display full week name(Ex: Wednesday)

Example if Date is 06 07 2016

SimpleDateFormat sdf= new SimpleDateFormat("dd MM yyyy");

you can use comma, full-stop, slash, or hyphen between these format.

 Ex: dd-mm-yyyy, it will display like(06-07-2016)
 dd/mm/yyyy,it will display like(06/07/2016)
 dd.mm.yyyy,it will display like(06.07.2016)
 dd,mm,yyyy ,it will display like(06,07,2016)

 MM   - will display number of the Month.
 MMM  - will display Month Three character only(Ex: Jul)
 MMMM - will display full month(Ex: July)

 yyyy - will display full year(2016) 
 yy   - will display last two digits(16)

 hh - will display hours
 mm -will display minutes
 ss - will display seconds
 a - will display AM or PM

 Ex: if time is 12:09:10 PM means (hh:mm:ss a)

 EEE- will display short week name(Ex: Wed)
 EEEE- will display full week name(Ex: Wednesday)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文