Getting the lastday of previous month

R

ruds

hi,
I want to get the last day of previous month.
i have tried using GregorianCalendar;
Calendar cal = new GregorianCalendar(year1, lastmonth, 1);

// Get the number of days in that month
cal.set(Calendar.MONTH, lastmonth);
//cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-1);
int days = cal.getActualMaximum(cal.DAY_OF_MONTH);
System.out.println("max = "+days);
String
lastdate=String.valueOf(cal.getActualMaximum(cal.DAY_OF_MONTH));
but still I get the lastdate for current month (31) and not for last
month(30).
Please tell me what to do?
 
J

Jens Seiler

ruds said:
hi,
I want to get the last day of previous month.
[...]
Please tell me what to do?

Set your calendar instance to the first day of the *current* month and
substract one day?

Greetings,

Jens Seiler
 
S

shakah

hi,
I want to get the last day of previous month.
i have tried using GregorianCalendar;
Calendar cal = new GregorianCalendar(year1, lastmonth, 1);

// Get the number of days in that month
cal.set(Calendar.MONTH, lastmonth);
//cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-1);
int days = cal.getActualMaximum(cal.DAY_OF_MONTH);
System.out.println("max = "+days);
String
lastdate=String.valueOf(cal.getActualMaximum(cal.DAY_OF_MONTH));
but still I get the lastdate for current month (31) and not for last
month(30).
Please tell me what to do?

Take a look at the "add(...)" method in java.util.Calendar, it might
be what your're looking for:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html

For example:
jim@sarah:/tmp$ cat lastday.java
public class lastday {
static public void main(String [] args) {
java.util.Calendar c = java.util.Calendar.getInstance() ;
c.set(java.util.Calendar.DAY_OF_MONTH, 1) ;
c.add(java.util.Calendar.DAY_OF_MONTH, -1) ;

System.out.println(java.text.DateFormat.getDateInstance().format(c.getTime())) ;
}
}

jim@sarah:/tmp$ /usr/bin/jdk1.5.0_09/bin/javac lastday.java

jim@sarah:/tmp$ /usr/bin/jdk1.5.0_09/bin/java lastday
Jun 30, 2008
 
R

ruds

Take a look at the "add(...)" method in java.util.Calendar, it might
be what your're looking for:
 http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html

For example:
jim@sarah:/tmp$ cat lastday.java
public class lastday {
  static public void main(String [] args) {
    java.util.Calendar c = java.util.Calendar.getInstance() ;
    c.set(java.util.Calendar.DAY_OF_MONTH, 1) ;
    c.add(java.util.Calendar.DAY_OF_MONTH, -1) ;

System.out.println(java.text.DateFormat.getDateInstance().format(c.getTime(­))) ;
  }

}

jim@sarah:/tmp$ /usr/bin/jdk1.5.0_09/bin/javac lastday.java

jim@sarah:/tmp$ /usr/bin/jdk1.5.0_09/bin/java lastday
Jun 30, 2008

this will give me only for 30th of month or 29th if current month has
30 days,
I want to use it every month without making changes to it.
 
R

ruds

Take a look at the "add(...)" method in java.util.Calendar, it might
be what your're looking for:
 http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html

For example:
jim@sarah:/tmp$ cat lastday.java
public class lastday {
  static public void main(String [] args) {
    java.util.Calendar c = java.util.Calendar.getInstance() ;
    c.set(java.util.Calendar.DAY_OF_MONTH, 1) ;
    c.add(java.util.Calendar.DAY_OF_MONTH, -1) ;

System.out.println(java.text.DateFormat.getDateInstance().format(c.getTime(­))) ;
  }

}

jim@sarah:/tmp$ /usr/bin/jdk1.5.0_09/bin/javac lastday.java

jim@sarah:/tmp$ /usr/bin/jdk1.5.0_09/bin/java lastday
Jun 30, 2008

Its not working for me...
java.util.Calendar c = java.util.Calendar.getInstance() ;
c.set(java.util.Calendar.DAY_OF_MONTH, 1) ;
c.add(java.util.Calendar.DAY_OF_MONTH, -1) ;
int days =Integer.valueOf(c.add(java.util.Calendar.DAY_OF_MONTH, -1));

gives error...
'void' type not allowed here .... for 'c.add....'
 
R

ruds

ruds said:
Take a look at the "add(...)" method in java.util.Calendar, it might
be what your're looking for:
 http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html
For example:
jim@sarah:/tmp$ cat lastday.java
public class lastday {
  static public void main(String [] args) {
    java.util.Calendar c = java.util.Calendar.getInstance() ;
    c.set(java.util.Calendar.DAY_OF_MONTH, 1) ;
    c.add(java.util.Calendar.DAY_OF_MONTH, -1) ;
System.out.println(java.text.DateFormat.getDateInstance().format(c.getTime(­­))) ;
  }
}
jim@sarah:/tmp$ /usr/bin/jdk1.5.0_09/bin/javac lastday.java
jim@sarah:/tmp$ /usr/bin/jdk1.5.0_09/bin/java lastday
Jun 30, 2008
Its not working for me...
java.util.Calendar c = java.util.Calendar.getInstance() ;
c.set(java.util.Calendar.DAY_OF_MONTH, 1) ;
c.add(java.util.Calendar.DAY_OF_MONTH, -1) ;
int days =Integer.valueOf(c.add(java.util.Calendar.DAY_OF_MONTH, -1));
gives error...
'void' type not allowed here .... for 'c.add....'

Of course.  Did you notice that Calendar#add() does not return a value?  What
did you imagine you were passing to the 'valueOf()' method?

Also, you invoked add() twice.  Once before the the valueOf(), and once
(incorrectly) inside it.

Read the Javadocs on java.util.Calendar to find out how to retrieve values
from it.  'add()' is not one of the ways.

Thanks Shakah and Lew,
I got that point correct.

Sorry,
did'nt read the example properly.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top