日期格式化程序未正确格式化日期
我编写了以下 java 代码,以特定格式格式化日期和时间。您可以在 ideone 中看到以下代码。
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
class timeAndDateTransformation{
public static void main(String[] argv){
Calendar newDate = new GregorianCalendar(2009,7,1,15,20,00);
SimpleDateFormat dateFormant = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat timeFormant = new SimpleDateFormat("HH:mm:ss");
System.out.println(dateFormant.format(newDate.getTime()).toString());
System.out.println(timeFormant.format(newDate.getTime()).toString());
}
它
给了我以下输出:
2009/08/01
15:20:00
在这个输出中,除了月份之外,其他一切都很好。我一个月通过了 7,但在这个格式输出中,它给出了 8 作为输出。请指出我哪里做错了。我对java的日期/日历类不是很熟悉,所以请耐心等待。
I wrote this following java code to format the date and time in specific formats. You can see the below code at ideone .
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
class timeAndDateTransformation{
public static void main(String[] argv){
Calendar newDate = new GregorianCalendar(2009,7,1,15,20,00);
SimpleDateFormat dateFormant = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat timeFormant = new SimpleDateFormat("HH:mm:ss");
System.out.println(dateFormant.format(newDate.getTime()).toString());
System.out.println(timeFormant.format(newDate.getTime()).toString());
}
}
Its giving me following output:
2009/08/01
15:20:00
In this output rest all it perfectly okay, except the month. I passed 7 as a month but in this for-matter output its giving 8 as a output. Please point me where am i doing wrong. I am not very familiar with the date/calendar classes of java, so please bear with me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
月份是从 0 开始的,您传入了 7,因此解析为八月。
来自 java.util.Date 的 api 文档:
让月份从零开始确实是违反直觉的。我想我们都曾在某个时刻被那个人烧伤过。
Months are 0-based, you passed in 7 so that resolves to August.
From the api docs for java.util.Date:
It's really counter-intuitive to make the month zero-based. I think we've all gotten burned by that one at some point.
Java 中的月份字段是从零开始的。 GregorianCalendar.JANUARY 是 0...等等等等。
因此,如果要将日期传递到构造函数中,请将月份值加一。
如果您查看 [JavaDoc 此处][1],它会为您解释。
[1]: http:// /download.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#set(int, int, int)
The month field in Java is zero based. GregorianCalendar.JANUARY is 0...etc etc.
Therefore, if you want to pass in a date into the constructor, add one to the month value.
If you look at the [JavaDoc here][1], it explains it for you.
[1]: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#set(int, int, int)
人们喜欢看到第一个月(一月)为 1,所以这就是
SimpleDateFormat
所做的。然而,计算机喜欢从 0 开始查看事物,这就是 GregorianCalendar 管理
month
参数的方式。请参阅 GregorianCalendar 的构造函数 和month
参数的描述。Humans like to see the first month (January) being 1, so that's what
SimpleDateFormat
does.However computers like to see things starting from 0, and that's how
GregorianCalendar
manages themonth
param. See the constructors for GregorianCalendar and the description of themonth
parameter.[Java 认为一月为 0。][1] 但是当您使用 SimpleDateFormat 输出月份数字时,它使用标准的一月为 1 月系统。因此,第 7 个月将输出为 8。
如果您对 JDK 的日期和日历有疑问,请考虑使用 Joda Time,更容易
[1]: http://download.oracle.com/javase/1.4.2/docs/api/java/util/GregorianCalendar.html#GregorianCalendar(int, int, int)
[Java considers January month 0. ][1] But when you output the month number with SimpleDateFormat it uses the standard January is month 1 system. So month 7 is outputted as 8.
If you are having trouble with the JDK's date and calendar consider using Joda Time, it's much easier
[1]: http://download.oracle.com/javase/1.4.2/docs/api/java/util/GregorianCalendar.html#GregorianCalendar(int, int, int)