aspose file tools*
The moose likes Java in General and the fly likes Current date - 10 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Current date - 10" Watch "Current date - 10" New topic
Author

Current date - 10

Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
Hi
I have a small problem with dates, like i have one date object,
i want to go back to 10 days before of that date,
ex : current date = 10/1/2008
i need date = 1/12/2007

can anyone let me know how can we do this.
appriciate for any helps...

-Naresh

Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Take a look in the Calendar class, specially on the add(int field, int amount)method
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Naresh,

have a look at class java.util.Calendar! There you have a method add() to add (or subtract) arbitrary values to or from arbitrary fields of a date (seconds, minutes etc.).

Marco
Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
hi
thanks alot i have tried like add(Calender.MONTH,-10);
now it is working fine..

Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
If you want to keep things simple you can do this using the Java Date class. Just create a new Date object with the default constructor, get the time in milliseconds from it, subtract 10 days (converted to millis) from that time, then use that time to create a new Date.

Put this in a main loop to see what I mean:




If you need to do anything more complicated with Dates in Java you probably want to check out the Calendar and GregorianCalendar classes.
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
I don't agree that this is simpler than

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14348
    
  22

Originally posted by Naresh Talluri:
thanks alot i have tried like add(Calender.MONTH,-10);

That will ofcourse subtract 10 months, not 10 days (as you asked) from the date.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148

I don't agree that this is simpler than

code:

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -10);


Rodrigo, you're right that code is simpler by one line (two of mine were print statements). I think that the extra line of code using Date objects makes it more explicit and serves as a clearer example to learn from without looking into the API.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Bill, I have three letters for you which will invalidate your code: DST. Remember, not all days of the year have 24 hours - one has 23 and one has 25. That's why when performing date calculations you should always use Calendar, since it knows how to handle DST.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Originally posted by Bill Cruise:


Rodrigo, you're right that code is simpler by one line (two of mine were print statements). I think that the extra line of code using Date objects makes it more explicit and serves as a clearer example to learn from without looking into the API.


I didn't mean simpler because of the number of lines.
I just think that is better to use the API than making those miliseconds calculation. And even easier to understand.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Another reason (not that you should need one) to avoid the Date + 10 * 24 * 60 * 60 * 1000 solution is that the entire class has been deprecated.

Calendar is what to use going forward, you might as well get used to using it now.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Bill Shirley]: Another reason (not that you should need one) to avoid the Date + 10 * 24 * 60 * 60 * 1000 solution is that the entire class has been deprecated.

No, the Date class has certainly not been deprecated. Many of its methods (and constructors) have been deprecated. But the class as a whole is definitely not deprecated, and it remains the preferred way to refer to dates and times in most Java programming contexts.

[Bill Shirley]: Calendar is what to use going forward, you might as well get used to using it now.


Calendar is what to use when you absolutely have to, because you can't find what you need in Date or DateFormat. Generally that means you need it for anything that might depend on knowing how many days are in a given month, or when daylight savings takes effect, or when leap years occur. Unfortunately Calendar has a very poorly-designed API, so I recommend avoiding it except when you really need it. The current problem of subtracting ten days probably qualifies - depending on what we mean by a "day". Is it 24 hours, or is it a calendar day? Usually people mean the latter, in which case you need Calendar. Or you could use Joda-Time, which is much nicer.


"I'm not back." - Bill Harding, Twister
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14348
    
  22

Originally posted by Bill Shirley:
Calendar is what to use going forward, you might as well get used to using it now.

The Java date and time APIs are one of those things that a lot of people are not really happy with, and that's why the Joda Time library that Jim already mentioned is popular.

In a future version of Java, there will probably be a new date and time API that will look approximately like Joda Time - see JSR 310. So I wouldn't say that the Calendar class is the future, but rather Joda Time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Current date - 10