• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.util.Date

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic