Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Getting the lastday of previous month

Reply
Thread Tools

Getting the lastday of previous month

 
 
ruds
Guest
Posts: n/a
 
      07-12-2008
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.D AY_OF_MONTH));
but still I get the lastdate for current month (31) and not for last
month(30).
Please tell me what to do?
 
Reply With Quote
 
 
 
 
Jens Seiler
Guest
Posts: n/a
 
      07-12-2008
ruds wrote:
> 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

--
man mailt sich
(E-Mail Removed) - ICQ# 24778881
http://www.jens-seiler.de
 
Reply With Quote
 
 
 
 
shakah
Guest
Posts: n/a
 
      07-12-2008
On Jul 12, 2:37 am, ruds <(E-Mail Removed)> wrote:
> 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.D AY_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/.../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.getDateIns tance().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
 
Reply With Quote
 
ruds
Guest
Posts: n/a
 
      07-12-2008
> 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/.../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.getDateIns tance().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.

 
Reply With Quote
 
ruds
Guest
Posts: n/a
 
      07-12-2008
> 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/.../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.getDateIns tance().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_M ONTH, -1));

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



 
Reply With Quote
 
ruds
Guest
Posts: n/a
 
      07-14-2008
On Jul 12, 6:13*pm, Lew <(E-Mail Removed)> wrote:
> ruds wrote:
> >> 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/.../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.getDateIns tance().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_M ONTH, -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.
>
> --
> Lew- Hide quoted text -
>
> - Show quoted text -


Thanks Shakah and Lew,
I got that point correct.

Sorry,
did'nt read the example properly.
Thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get first/last day of the previous month? Hussein B Python 12 01-21-2009 08:56 AM
Get Previous month Pragash Mr. Ruby 2 08-06-2008 11:30 AM
Figure out month number from month abbrievation Bill Python 10 04-13-2006 12:36 PM
MonthCalendar1 or DateTimePicker1 How to jump for one month or haw to get no of month? Michal Maciejczak C++ 1 04-23-2005 11:18 AM
to convert a month to previous month eddie wang ASP General 9 10-09-2003 11:15 PM



Advertisments