File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes java.util.Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.util.Date" Watch "java.util.Date" New topic


kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
a couple of questions about Dates. The most pressing one is this : I have today's date, and I have it nicely formatted. Call it theDate. How can I use get at yesterday's date, without introducing a new variable, and without changing the value of theDate ?
The less urgent problem is that I'm using these deprecated methods, getDate and setDate, and I don't understand how I can do much without them - can anyone suggest an easy explanation or something to read ?
Many thanks,
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
import java.util.Date;
import java.text.*;
public class GetDate
public static void main(String arg[])
Date now = new Date();

System.out.println("right now is : " + now.toString());
You won't get depreciation while compiling the above code
for more information read the

Let me know if you figure out how to get yesterday?
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Hi Mindy,
It's really when I do the yesterday thing that I run into problems... using your 'now', I can do :

to make 'now' into yesterday's date. However, this is where setDate and getDate are the deprecated methods. (and also, where I would like to get at yesterday's date without affecting 'now')
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Kate,
I don't know about keeping the original value the same, but you should be using java.util.Calendar. Look specifically at the add method. The API shows how to subtract using it ...
A Agrawal
Ranch Hand

Joined: Jul 13, 2001
Posts: 41
I don't know whether you would like this solution or not...but one simple and non deprecated way of doing it -
Date dtNow = new Date(); ///this is your 'formatted date'
Date dtYesterday = new Date(); //last date
dtYesterday.setTime(dtNow.getTime() - 24 * 60 * 60 * 1000);

Amit Agrawal,
New Delhi, India.

Amit Agrawal,<BR>New Delhi, India.
I agree. Here's the link:
subject: java.util.Date
It's not a secret anymore!