[英]converting a calendar object into a string in java with format “yyyy-mm-dd hh:mm:ss”
I'm converting a date stored in a calendar object in a string for a query in MySQL. 我正在将一个日历对象中存储的日期转换为MySQL中的查询。 I need the string in format "yyyy-MM-dd HH:mm:ss", ie: "2010-01-01 15:30:00".
我需要格式为“yyyy-MM-dd HH:mm:ss”的字符串,即:“2010-01-01 15:30:00”。 I'm using a code like:
我正在使用如下代码:
Calendar TimeStop = Calendar.getInstance();
TimeStop.set(2010, 01, 01, 15, 30, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String TimeStop_Str = sdf.format(TimeStop.getTime());
now, the string, instead of being "2010- 01 -01 15:30:00" like I would expect is "2010- 02 -01 15:30:00". 现在,该字符串的,而不是“2010 - 01 -01 15:30:00”像我所期望的是“2010 - 02 -01 15:30:00”。 I have already checked on http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for possible errors in the parser formula (for example the capital MM for month or capital HH for hours) but it didn't worked.
我已经在http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html上检查了解析器公式中可能存在的错误(例如,月份或资本HH的资本MM)几个小时)但它没有奏效。
I guess that there is some other field that I should set, or maybe there is another method... any idea? 我想我应该设置一些其他领域,或者可能还有另一种方法......任何想法?
Calendar.JANUARY
is actually 0
, not 1
. Calendar.JANUARY
实际上是0
,而不是1
。
When you provide 01
for the month field in set
, you're actually setting the month to February, which is why you get 02
when SimpleDateFormat
renders it as MM
. 当您为
set
的月份字段提供01
,您实际上将月份设置为2月,这就是当SimpleDateFormat
将其呈现为MM
时获得02
原因。
When you use any of the Calendar.get/set
methods, you must take extra precautions to make sure that you are aware of this discrepancy between the "natural" 1-based indexing, and the more "awkward" Calendar
's 0-based indexing. 当您使用任何
Calendar.get/set
方法时,您必须采取额外的预防措施,以确保您了解“自然的”基于1的索引与更“笨拙”的基于0的Calendar
之间的差异索引。 Any time you're getting/setting the month of a Calendar
, there's always this potential to cause a serious bug. 每当你获得/设置一个
Calendar
的月份时,总有这种可能导致严重的错误。
This is just one of those really awkward design in Calendar
that leaves a lot to be desired. 这只是
Calendar
中那些非常笨拙的设计之一,还有很多不足之处。 One of the better, more pleasant date/time API library available out there is Joda Time , so if at all possible, you may consider a switch to that library instead. 其中一个更好,更愉快的日期/时间API库是Joda Time ,所以如果可能的话,你可以考虑转换到那个库。
Calendar.MONTH
- "Field number for get
and set
indicating the month. This is a calendar-specific value. The first month of the year [...] is JANUARY
which is 0". Calendar.MONTH
- “为现场数get
和set
。指示一个月这是一个特定于日历的值一年的第一个月[...]是JANUARY
是0”。 Calendar.set(…, int month, …)
- " month
- the value used to set the MONTH
calendar field. Month value is 0-based. eg, 0 for January." Calendar.set(…, int month, …)
- “ month
- 用于设置MONTH
日历字段的值。月值为0,例如,0表示1月。” Also, note that 01
is actually an octal literal (ie base 8 ). 另外,请注意
01
实际上是八进制文字(即基数为8 )。 You shouldn't make a habit of prepending 0
to integer literals ( §3.10.1 ), because they can cause subtle bugs/errors unless you're very careful. 你不应该养成将
0
添加到整数文字( §3.10.1 )的习惯,因为它们会导致细微的错误/错误,除非你非常小心。
For example, int i = 09;
例如,
int i = 09;
is an illegal Java code. 是一种非法的Java代码。
System.out.println(010); // prints 8, not 10
It's just that months start from 0
(not 1
) 这只是几个月从
0
开始(不是1
)
That said, you should not convert to string in order to insert a date in the DB. 也就是说,您不应该转换为字符串以便在数据库中插入日期。 You can either use the timestamp, or
java.sql.Date
together with PreparedStatement
您可以将时间戳或
java.sql.Date
与PreparedStatement
一起使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.