Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do I calculate today's date minus one month?

Reply
Thread Tools

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

 
 
laredotornado
Guest
Posts: n/a
 
      10-03-2008
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
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      10-03-2008
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
 
Reply With Quote
 
 
 
 
Tim Slattery
Guest
Posts: n/a
 
      10-03-2008
laredotornado <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed)
http://members.cox.net/slatteryt
 
Reply With Quote
 
laredotornado
Guest
Posts: n/a
 
      10-03-2008
On Oct 3, 10:37*am, Tim Slattery <(E-Mail Removed)> wrote:
> laredotornado <(E-Mail Removed)> 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
> (E-Mail Removed)://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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-06-2008
On Fri, 3 Oct 2008 07:36:55 -0700 (PDT), laredotornado
<(E-Mail Removed)> 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
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Calculate date based on another date + number of minutes Golan C Programming 3 09-15-2006 11:32 PM
Calculate the date after subtracting nmbr of days form a date Laery C Programming 11 02-25-2005 07:59 AM
how to minus a date? =?Utf-8?B?QXNoYQ==?= ASP .Net 2 01-18-2005 11:55 PM
minus one date from another joeandtel ASP General 1 02-04-2004 05:12 PM



Advertisments