Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1020759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:12:25+00:00 2026-05-16T11:12:25+00:00

I’m converting a date stored in a calendar object in a string for a

  • 0

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:

    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.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.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:12:25+00:00Added an answer on May 16, 2026 at 11:12 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.