• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Getting the last day of a certain month!

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
nagunpyo
Asked:
nagunpyo
  • 8
  • 6
  • 5
1 Solution
 
CEHJCommented:
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
 
CEHJCommented:
(Note that Java months are zero-indexed. I've set the month number there to the 'normal' index)
0
 
nagunpyoAuthor Commented:
i just need a simple interger return value. lol
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
nagunpyoAuthor Commented:
int getLastDay(int year, int month){
...
}
0
 
nagunpyoAuthor Commented:
I know I could get the last day with simple if statments but apprently, i have problems with Feb.
0
 
zzynxSoftware engineerCommented:
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
 
zzynxSoftware engineerCommented:
       System.out.println(getLastDayOfMonth(2005,2));   // prints out 28
        System.out.println(getLastDayOfMonth(2005,3));   // 31
        System.out.println(getLastDayOfMonth(2005,4));   // 30
0
 
nagunpyoAuthor Commented:
cool! does it work in Java script as well?
cuz I actually need it for my web site...
0
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
>> does it work in Java script as well?
That code is java code, not java script code
0
 
zzynxSoftware engineerCommented:
>> I actually need it for my web site
You can use that code in a java applet
0
 
nagunpyoAuthor Commented:
Any workaround for using it as a plain jave script?
0
 
zzynxSoftware engineerCommented:
0
 
CEHJCommented:
nagunpyo i've given you the answer to this question in Java. This is the Java TA, not JAvaScript
0
 
nagunpyoAuthor Commented:
I'm sorry, I placed the quetion in the wrong section.
Anyways, here's the point you deserve. :-)
0
 
zzynxSoftware engineerCommented:
cool!
;°(
0
 
CEHJCommented:
:-)
0
 
CEHJCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 8
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now