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 6781995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:41:20+00:00 2026-05-26T16:41:20+00:00

I have following code to get different parts of current system Date (10-11-2011 for

  • 0

I have following code to get different parts of current system Date (10-11-2011 for this case).

Calendar now = Calendar.getInstance();
String dt = ""+now.get(now.DATE)+"-"+now.get(now.MONTH)+"-"+now.get(now.YEAR);

Here, DATE and YEAR fields are giving values as expected but MONTH field is giving unexpected results, firstly I didn’t knew that MONTH field starts with zero, so having current month as 11 will give me 10. Now, if I use now.get(now.MONTH+1) than it returns 46. And using simply now.MONTH instead of using get method gives 2.

So, what am I doing wrong here? it shouldn’t be a bug in Calendar class.

Note that I’m using JDK 7.

  • 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-26T16:41:21+00:00Added an answer on May 26, 2026 at 4:41 pm

    You need now.get(Calendar.MONTH) + 1.

    now.get(Calendar.MONTH) returns the month starting at 0. And you need to add 1 to the result. If you do now.get(Calendar.MONTH + 1), you’re getting something other than the month, because you don’t pass the MONTH constant to the get method anymore. get just takes an int as parameter. The constant MONTH means “I want to get the month”. The constant DATE means “I want to get the date”. Their value has no meaning. MONTH is 2, and 3 is WEEK_OF_YEAR.

    Also note that static variables (or constants) should be accessed using the class name, and not an instance of the class.

    Have you considered using SimpleDateFormat? That’s the class to use to format a Date using a specific pattern:

    new SimpleDateFormat("dd/MM/yyyy").format(now.getTime());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
I have the following code: // GET: /PlayRoundHole/Create public ActionResult Create(int id) { DB
I have the following code to get the object type of a collection: Dim
I have the following code to get details of a contact. data: is the
I have following code that does not work: I never get to goToFoodDetail .
I have a following simple code: def get(): return [lambda: i for i in
I have written a following code to get just the file name without extension
I have used the following code to get the document root. $path = get_file_dir();
I have the following code: $.ajax({ type: 'GET', url: 'index.php?route=checkout/onepagecheckout/getpaypaldata', dataType: 'json', success: function(json)
I have now tried to get this to work for a few hours but

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.