[英]Java date format conversion - getting wrong month
I have a problem in converting the date in java, don't know where i am going wrong...我在转换 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();
}
Input date is 2011-12-15 and I am expecting the result as "15 December 2011", but I get it as "15 January 2011"输入日期是 2011-12-15,我期望结果是“2011 年 12 月 15 日”,但我得到的结果是“2011 年 1 月 15 日”
where am I going wrong?我哪里错了?
您的fromFormat
使用分钟,而它应该使用几个月。
String fromFormat = "yyyy-MM-dd";
I think the fromFormat should be "yyyy-MM-dd".我认为fromFormat应该是“yyyy-MM-dd”。
Here is the format:这是格式:
More: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html更多: http : //docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Look at the javadoc of SimpleDateFormat
and look at what the m
represents.查看SimpleDateFormat
的 javadoc 并查看m
代表什么。 Not months as you think but minutes.不是你想象的几个月,而是几分钟。
String fromFormat = "yyyy-MM-dd";
从格式应该是:
String fromFormat = "yyyy-MM-dd"
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.
15 December 2011 2011 年 12 月 15 日
While the accepted Answer is correct (uppercase MM
for month), there is now a better approach.虽然接受的答案是正确的(大写MM
表示月份),但现在有更好的方法。 The troublesome old date-time classes are now legacy, supplanted by the java.time classes.麻烦的旧日期时间类现在是遗留的,被 java.time 类取代。
Your input string is in standard ISO 8601 format.您的输入字符串采用标准ISO 8601格式。 So no need to specify a formatting pattern for parsing.所以不需要为解析指定格式模式。
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 );
Dump to console.转储到控制台。
System.out.println( "ld.toString(): " + ld );
System.out.println( "output: " + output );
ld.toString(): 2011-12-15 ld.toString(): 2011-12-15
output: 15 December 2011产出:2011年12月15日
See live code in IdeOne.com .在 IdeOne.com 中查看实时代码。
The java.time framework is built into Java 8 and later. java.time框架内置于 Java 8 及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.这些类取代麻烦的老传统日期时间类,如java.util.Date
, Calendar
,和SimpleDateFormat
。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial .要了解更多信息,请参阅Oracle 教程。 And search Stack Overflow for many examples and explanations.并在 Stack Overflow 上搜索许多示例和解释。 Specification is JSR 310 .规范是JSR 310 。
You may exchange java.time objects directly with your database.您可以直接与您的数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later.使用符合JDBC 4.2或更高版本的JDBC 驱动程序。 No need for strings, no need for java.sql.*
classes.不需要字符串,不需要java.sql.*
类。
Where to obtain the java.time classes?从哪里获得 java.time 类?
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目用额外的类扩展了 java.time。 This project is a proving ground for possible future additions to java.time.该项目是未来可能添加到 java.time 的试验场。 You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more .您可以在这里找到一些有用的类,比如Interval
, YearWeek
, YearQuarter
,和更多。
m
in SimpleDateFormat
stands for minutes, while M
stands for month. SimpleDateFormat
m
代表分钟,而M
代表月份。 Thus your first format should be yyyy-MM-dd
.因此,您的第一个格式应该是yyyy-MM-dd
。
Well this may not be your case but may help someone.那么这可能不是你的情况,但可能会帮助某人。 In my case after conversion, day of month and month set 1. So whatever date is, after conversion i get 1 jan which is wrong.在我的情况下,转换后,月份的日期和月份设置为 1。所以无论日期是什么,转换后我得到 1 jan,这是错误的。 After struggling i found that in date format i have used YYYY
instead of yyyy
.经过努力,我发现在日期格式中我使用了YYYY
而不是yyyy
。 When i changed all caps Y to y it works fine.当我将所有大写 Y 更改为 y 时,它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.