将日历对象转换为java中的字符串,格式为“yyyy-mm-dd hh:mm:ss”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3583384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:09:15  来源:igfitidea点击:

converting a calendar object into a string in java with format "yyyy-mm-dd hh:mm:ss"

javadatetime

提问by andystrath

I'm converting a date stored in a calendar object in a string for a query in MySQL. I need the string in format "yyyy-MM-dd HH:mm:ss", i.e.: "2010-01-01 15:30:00". I'm using a code like:

我正在转换存储在日历对象中的字符串中的日期,以便在 MySQL 中进行查询。我需要格式为“yyyy-MM-dd HH:mm:ss”的字符串,即:“2010-01-01 15:30:00”。我正在使用如下代码:

    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". I have already checked on http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.htmlfor possible errors in the parser formula (for example the capital MM for month or capital HH for hours) but it didn't worked.

现在,该字符串的,而不是“2010 - 01-01 15:30:00”像我所期望的是“2010 - 02-01 15:30:00”。我已经在http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html 上检查了解析器公式中可能存在的错误(例如大写 MM 表示月份或大写 HH几个小时)但它没有用。

I guess that there is some other field that I should set, or maybe there is another method... any idea?

我想我应该设置其他一些字段,或者可能有另一种方法......知道吗?

采纳答案by polygenelubricants

Calendar.JANUARYis actually 0, not 1.

Calendar.JANUARY实际上0,不是1

When you provide 01for the month field in set, you're actually setting the month to February, which is why you get 02when SimpleDateFormatrenders it as MM.

当你提供01的是当月领域set,你每月实际设置至2月,这就是为什么你02SimpleDateFormat它呈现为MM

When you use any of the Calendar.get/setmethods, 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. Any time you're getting/setting the month of a Calendar, there's always this potential to cause a serious bug.

当您使用任何一种Calendar.get/set方法时,您必须采取额外的预防措施,以确保您意识到“自然”的基于 1 的索引与更“尴尬”Calendar的基于 0 的索引之间的差异。任何时候获取/设置 a 的月份,Calendar总是有可能导致严重的错误。

This is just one of those really awkward design in Calendarthat leaves a lot to be desired. 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.

这只是那些非常尴尬的设计之一Calendar,还有很多不足之处。可用的更好、更令人愉快的日期/时间 API 库之一是Joda Time,因此如果可能,您可以考虑改用该库。

API links

接口链接

  • Calendar.MONTH- "Field number for getand setindicating the month. This is a calendar-specific value. The first month of the year [...] is JANUARYwhich is 0".
  • Calendar.set(…, int month, …)- "month- the value used to set the MONTHcalendar field. Month value is 0-based. e.g., 0 for January."
  • Calendar.MONTH- “字段编号getset指示月份。这是一个日历特定的值。一年中的第一个月 [...]JANUARY是 0”。
  • Calendar.set(…, int month, …)- " month- 用于设置MONTH日历字段的值。月份值从 0 开始。例如,0 表示一月。"


On octal literals

关于八进制文字

Also, note that 01is actually an octal literal (i.e. base 8). You shouldn't make a habit of prepending 0to integer literals (§3.10.1), because they can cause subtle bugs/errors unless you're very careful.

另外,请注意这01实际上是一个八进制文字(即基数 8)。您不应该养成0在整数文字(§3.10.1)前面加上的习惯,因为除非您非常小心,否则它们会导致细微的错误/错误。

For example, int i = 09;is an illegal Java code.

例如,int i = 09;是非法的Java 代码。

    System.out.println(010); // prints 8, not 10

See also

也可以看看

回答by Bozho

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.Datetogether with PreparedStatement

也就是说,您不应该为了在数据库中插入日期而转换为字符串。您可以使用时间戳,也可以java.sql.DatePreparedStatement