?
Solved

Getting the last day of a certain month!

Posted on 2005-04-29
19
Medium Priority
?
250 Views
Last Modified: 2010-03-31
I'm looking for a JAVA method which, I can pass the year and the month and it will return the last day of that month!

0
Comment
Question by:nagunpyo
  • 8
  • 6
  • 5
19 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 13893853
Last day of this month:

                  int month = 4;
                  Calendar cal = Calendar.getInstance();
                  cal.set(Calendar.YEAR, 2005);
                  cal.set(Calendar.MONTH, month);
                  cal.set(Calendar.DATE, 1);
                  cal.add(Calendar.DATE, -1);
                  System.out.println(cal.getTime());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13893858
(Note that Java months are zero-indexed. I've set the month number there to the 'normal' index)
0
 

Author Comment

by:nagunpyo
ID: 13893926
i just need a simple interger return value. lol
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:nagunpyo
ID: 13893932
int getLastDay(int year, int month){
...
}
0
 

Author Comment

by:nagunpyo
ID: 13893940
I know I could get the last day with simple if statments but apprently, i have problems with Feb.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13893968
public int getLastDay(int year, int month){
               Calendar cal = Calendar.getInstance();
               cal.set(Calendar.YEAR, year);
               cal.set(Calendar.MONTH, month);
               cal.set(Calendar.DATE, 1);
               cal.add(Calendar.DATE, -1);
               return cal.get(Calendar.DATE);  
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13894004
       System.out.println(getLastDayOfMonth(2005,2));   // prints out 28
        System.out.println(getLastDayOfMonth(2005,3));   // 31
        System.out.println(getLastDayOfMonth(2005,4));   // 30
0
 

Author Comment

by:nagunpyo
ID: 13894025
cool! does it work in Java script as well?
cuz I actually need it for my web site...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13894039
>> I know I could get the last day with simple if statments but apprently, i have problems with Feb.
You'd better rely on the Calendar class's knowledge of leap years
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13894049
>> does it work in Java script as well?
That code is java code, not java script code
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13894059
>> I actually need it for my web site
You can use that code in a java applet
0
 

Author Comment

by:nagunpyo
ID: 13894078
Any workaround for using it as a plain jave script?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13894113
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13894147
nagunpyo i've given you the answer to this question in Java. This is the Java TA, not JAvaScript
0
 

Author Comment

by:nagunpyo
ID: 13894189
I'm sorry, I placed the quetion in the wrong section.
Anyways, here's the point you deserve. :-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13894296
cool!
;°(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13894443
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13894455
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

807 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