Solved

Why is it that Java calendar only has 28 days for January 2004?

Posted on 2003-11-02
8
286 Views
Last Modified: 2010-03-31
I tried using Java Calendar and I realized that January 2004 has only 28 days.
I added 2 days to the date 28 January 2004 , but the returned date is 2 Feb 2004...
Y is it so?

0
Comment
Question by:agamarjo
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9668238
post your code
0
 
LVL 2

Expert Comment

by:smitty22
ID: 9668311
Remember, it's zero-based:

0 = january
1 = february
2 = may
0
 
LVL 2

Expert Comment

by:smitty22
ID: 9668316
2 = march, that is
0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 9672058
The following code snippet

      java.text.SimpleDateFormat dayFormatter =
        new java.text.SimpleDateFormat("dd/MM/yyyy");
      java.util.Calendar c = java.util.Calendar.getInstance();
      c.set(2004,0,28);
      c.add(java.util.Calendar.DATE, 2);
      System.out.println(dayFormatter.format(c.getTime()));

nicely prints out:

       30/01/2004

Hope this is helpful to you.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:jimmack
ID: 9672434
I still haven't seen a good reason, but smitty22 is right, months are numbered 0 .. 11.

Days and years use the actual values.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 9676770
Hi Agamarjo,
Thanks for the acceptance.

For helping me answering future questions better, can you tell me what I could have done better to deserve an A grade?

Kind regards.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9677322
That's a grade 'A' answer.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 9708211
Hi jimmack,
Thanks for sharing your opinion (and thus strengthen mine)
The fact that the author of the question keeps quiet proves s/he thinks the same way about it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java / Linux and Regular Expressions 11 69
Java JRE greater than 1.6 5 50
custom annotations 9 30
more than one jdk and one jre 1 31
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now