How do I calculate today's date minus one month?

Discussion in 'Java' started by laredotornado, Oct 3, 2008.

  1. Hi,

    I'm using Java 1.5. How do I get a java.util.Date exactly one month
    before today's date? I thought about converting todays date to
    milliseconds and then subtracting a value, but not every month has the
    same number of milliseconds.

    - Dave
    laredotornado, Oct 3, 2008
    #1
    1. Advertising

  2. laredotornado wrote:
    > Hi,
    >
    > I'm using Java 1.5. How do I get a java.util.Date exactly one month
    > before today's date? I thought about converting todays date to
    > milliseconds and then subtracting a value, but not every month has the
    > same number of milliseconds.


    Month as in "30 days" or "this day in the last calendar month"? What is
    exactly one month before December 31? March 29?

    Here's some code using Calendar that treats the equivalent of "November
    31" as "November 30":

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.MONTH, -1);
    date = cal.getTime();

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Oct 3, 2008
    #2
    1. Advertising

  3. laredotornado

    Tim Slattery Guest

    laredotornado <> wrote:

    >Hi,
    >
    >I'm using Java 1.5. How do I get a java.util.Date exactly one month
    >before today's date? I thought about converting todays date to
    >milliseconds and then subtracting a value, but not every month has the
    >same number of milliseconds.


    Look at the add method of GregorianCalendar, that will do what you
    need.

    --
    Tim Slattery

    http://members.cox.net/slatteryt
    Tim Slattery, Oct 3, 2008
    #3
  4. On Oct 3, 10:37 am, Tim Slattery <> wrote:
    > laredotornado <> wrote:
    > >Hi,

    >
    > >I'm using Java 1.5.  How do I get a java.util.Date exactly one month
    > >before today's date?  I thought about converting todays date to
    > >milliseconds and then subtracting a value, but not every month has the
    > >same number of milliseconds.

    >
    > Look at the add method of GregorianCalendar, that will do what you
    > need.
    >
    > --
    > Tim Slattery
    > ://members.cox.net/slatteryt


    What I meant by exactly one month is if today is "10-3-2008", one
    month ago was "9-3-2008". Of course, I don't know what one month ago
    means for "7-31-2008". But I have used GregorianCalendar's add
    method, per Tim's suggestion

    this.m_throughDate = new Date();
    GregorianCalendar gc = new
    GregorianCalendar(this.m_throughDate.getYear(),
    this.m_throughDate.getMonth(), this.m_throughDate.getDate());
    gc.add(Calendar.MONTH, -1);
    this.m_fromDate = gc.getTime();

    Thanks, - Dave
    laredotornado, Oct 3, 2008
    #4
  5. laredotornado

    Roedy Green Guest

    On Fri, 3 Oct 2008 07:36:55 -0700 (PDT), laredotornado
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I'm using Java 1.5. How do I get a java.util.Date exactly one month
    >before today's date? I thought about converting todays date to
    >milliseconds and then subtracting a value, but not every month has the
    >same number of milliseconds.


    That is trivial with BigDate. See TestDate examples. With Calendar is
    is a bit uglier. See http://mindprod.com/products1.html#COMMON11
    http://mindprod.com/jgloss/calendar.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com/politics/harper.html
    Anyone but Harper for Prime Minister of Canada
    Roedy Green, Oct 6, 2008
    #5
    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,978
    Matthias Kaeppler
    Apr 23, 2005
  2. Replies:
    1
    Views:
    445
    Steve W. Jackson
    Dec 22, 2006
  3. Mufasa
    Replies:
    0
    Views:
    397
    Mufasa
    Sep 5, 2008
  4. joeandtel

    minus one date from another

    joeandtel, Feb 4, 2004, in forum: ASP General
    Replies:
    1
    Views:
    210
    Aaron Bertrand - MVP
    Feb 4, 2004
  5. Mason Kessinger
    Replies:
    16
    Views:
    276
    David Brady
    Jan 20, 2006
Loading...

Share This Page