# Java Date class



## cpscdave (Feb 25, 2004)

Hi all, 
I'm doing this inside of iReport so a little limited in what exactly I can do.

But is there a method using the Date class to get Date -1 Month?

If I was using PHP I'd use: 

```
$date = time();
$prev_month = strtotime("-1 MONTH", $date);
$prev_month = strftime("%Y-%m", $prev_month);
```
So is there a way in Java this can be accomplished?? Oh and seeing how this is in iReport it kinda has to all be in 1 line lol

Thanks


----------



## NeonFx (Oct 22, 2008)

Hey there, how about using the JDateTime class seing as it works with integers, making it all the easier to add or subtract months using the addMonth method ('add' a negative month to subtract).

http://jodd.sourceforge.net/doc/jdatetime.html


----------



## cpscdave (Feb 25, 2004)

wish I could but I can't. I'm really limited to whats in iReport. So stuck w/ using the Date class. 

Right now its being set up to pass into the report the 12 months. I'd really love to simplify it to pass in just the start month and calculate the prior 11 months.


----------



## NeonFx (Oct 22, 2008)

I'm sorry, i have no idea what iReport is, or how to use it. If it uses java however, and compiles code, then you should be able to add libraries and packages to it, only i have no idea how to do that.

You could use the more common Calendar class which is probably included in the default libraries.

it also has an add class... use it as so:

*(Calendar cl).add(Calendar.MONTH, -1);* would subtract 1 month.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

If you can only use the Date class though... which i doubt... then just add a long series of if statements... If month == "December" month = "November";, etc.


----------

