my dog learned polymorphism
The moose likes Java in General and the fly likes Getting Previous Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting Previous Date" Watch "Getting Previous Date" New topic

Getting Previous Date

Rohit Dhodapkar
Ranch Hand

Joined: Apr 27, 2006
Posts: 38
I want to get a date which is n days previous to the current date. I want to do it without using the Calender Objet but just by using java.util.Date.

Can any body please help.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
Why do you want to do this without Calendar ?

Date has a constructor taking a long argument which is the number of milliseconds sinse 1.1.1970 (see javadoc). And if you have a Date X where you want to subtract 1 day, then its:
oldDate in millis - 1000 * 60 * 60 * 24

make sure the multiplication does not exceed the int range!

Rohit Dhodapkar
Ranch Hand

Joined: Apr 27, 2006
Posts: 38

I want to do this stuff in the java script .Can you please advice me how to do it?.

Thanks in advance
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14991

In JavaScript, not in Java?

The forums here are about Java. The JavaScript language is something different than Java, even though the names are similar.

If you have a question about JavaScript, you should ask your question somewhere else, in a forum about JavaScript.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Paul Clapham

Joined: Oct 14, 2005
Posts: 19739

We do have an HTML and Javascript forum here, which the post could be moved to. But Rohit originally asked about Java, then about Javascript. So maybe there is a question about which one Rohit should be using?
I agree. Here's the link:
subject: Getting Previous Date
It's not a secret anymore!