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

后端 未结 2 1450
故里飘歌
故里飘歌 2021-01-12 10:58

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

相关标签:
2条回答
  • 2021-01-12 11:44

    It's just that months start from 0 (not 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

    0 讨论(0)
  • 2021-01-12 11:47

    Calendar.JANUARY is actually 0, not 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.

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

    This is just one of those really awkward design in Calendar that 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.

    API links

    • 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.set(…, int month, …) - "month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January."

    On octal literals

    Also, note that 01 is actually an octal literal (i.e. base 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.

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

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

    See also

    • Can I customize syntax highlighting in Eclipse to show octal literals differently?
    • Octal number literals: when? why? ever?
    0 讨论(0)
提交回复
热议问题