简体   繁体   English

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

[英]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 ,所以如果可能的话,你可以考虑转换到那个库。

API links API链接

  • 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 - “为现场数getset 。指示一个月这是一个特定于日历的值一年的第一个月[...]是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月。”

On octal literals 八进制文字

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

See also 也可以看看

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.DatePreparedStatement一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java 7中将字符串日期转换为yyyy-MM-dd'T'HH:mm:ss.SSSSSS格式的字符串 - Converting string date to string in yyyy-MM-dd'T'HH:mm:ss.SSSSSS format in java 7 格式为“ yyyy-mm-dd HH:mm:ss.SSSS”的Java String to Date对象 - Java String to Date object of the format “yyyy-mm-dd HH:mm:ss.SSSS” 格式为“yyyy-mm-dd HH:mm:ss”的 Java String to Date 对象 - Java String to Date object of the format “yyyy-mm-dd HH:mm:ss” Java 格式 yyyy-MM-dd'T'HH:mm:ss.SSSz 到 yyyy-mm-dd HH:mm:ss - Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss Joda DateTime格式为yyyy-MM-DD HH:MM:SS - Joda DateTime format to yyyy-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS格式的时间值 - Time value in YYYY-MM-DD HH:MM:SS format 如何比较 java 格式“EEE MMM dd HH:mm:ss zzz yyyy”和“yyyy-MM-dd hh:mm:sss”的日期时间? - How to compare the datetime of format "EEE MMM dd HH:mm:ss zzz yyyy" and "yyyy-MM-dd hh:mm:sss" in java? 如何将 java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) 格式化为日期(yyyy-MM-dd HH:mm:ss) - How to format a java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) to a date(yyyy-MM-dd HH:mm:ss) 格式为“ MM / dd / yyyy HH:mm:ss a”的Java String to Date对象 - Java String to Date object of the format “MM/dd/yyyy HH:mm:ss a” 将字符串转换为“yyyy-MM-dd HH:mm:ss”时无法解析的日期异常 - Unparseable date exception when converting a string to “yyyy-MM-dd HH:mm:ss”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM