• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.Date

 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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,
Kate
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 http://java.sun.com/doc/books/tutorial/il8n/format/simpleDateFormat.html

Let me know if you figure out how to get yesterday?
Thanks!
 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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')
Cheers,
Kate
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
Regards,
Manfred.
 
A Agrawal
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know whether you would like this solution or not...but one simple and non deprecated way of doing it -
<code>
Date dtNow = new Date(); ///this is your 'formatted date'
Date dtYesterday = new Date(); //last date
dtYesterday.setTime(dtNow.getTime() - 24 * 60 * 60 * 1000);
</code>

------------------
Amit Agrawal,
New Delhi, India.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic