Getting the lastday of previous month

Discussion in 'Java' started by ruds, Jul 12, 2008.

  1. ruds

    ruds Guest

    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?
    ruds, Jul 12, 2008
    #1
    1. Advertising

  2. ruds

    Jens Seiler Guest

    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
    mailto: - ICQ# 24778881
    http://www.jens-seiler.de
    Jens Seiler, Jul 12, 2008
    #2
    1. Advertising

  3. ruds

    shakah Guest

    On Jul 12, 2:37 am, ruds <> 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.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
    shakah, Jul 12, 2008
    #3
  4. ruds

    ruds Guest

    > 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.
    ruds, Jul 12, 2008
    #4
  5. ruds

    ruds Guest

    > 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....'
    ruds, Jul 12, 2008
    #5
  6. ruds

    ruds Guest

    On Jul 12, 6:13 pm, Lew <> 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/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.
    >
    > --
    > Lew- Hide quoted text -
    >
    > - Show quoted text -


    Thanks Shakah and Lew,
    I got that point correct.

    Sorry,
    did'nt read the example properly.
    Thanks.
    ruds, Jul 14, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Maciejczak
    Replies:
    1
    Views:
    1,969
    Matthias Kaeppler
    Apr 23, 2005
  2. Bill
    Replies:
    10
    Views:
    718
    Fulvio
    Apr 13, 2006
  3. Replies:
    1
    Views:
    429
    Steve W. Jackson
    Dec 22, 2006
  4. Mufasa
    Replies:
    0
    Views:
    383
    Mufasa
    Sep 5, 2008
  5. eddie wang

    to convert a month to previous month

    eddie wang, Oct 9, 2003, in forum: ASP General
    Replies:
    9
    Views:
    113
    Chris Hohmann
    Oct 10, 2003
Loading...

Share This Page