• 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

Dates

 
Ranch Hand
Posts: 398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an object that needs to be passed with two dates. One is the system date and the other is 24 hrs behind system date. How do I calculate the second date? Do I convert the systemdate into time using getTime method? If I subtract the milliseconds for a day from this time, how do I get back date from it?


Thanks,
vasu
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's one possible approach. However, it is probably easier to use the Calendar class. It has methods to calculate a new Date object from an old one. Check out the Java API docs for details.

Layne
 
vasu maj
Ranch Hand
Posts: 398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any methods in Claneder class which let you do that. There is a question at the end of my original post. Can someone give me a clue please?
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Calendar there should be an add method for which you give -1 as input which means 1 day before and you get the previous date.

something like

Calendar calADD = java.util.GregorianCalendar.getInstance();
System.out.println( calADD.getTime() + " Time After 10 days before:" +
calADD.add(Calender.Date, -10) );

// 1 Month after
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 1);
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vasu maj:
I don't see any methods in Claneder class which let you do that. There is a question at the end of my original post. Can someone give me a clue please?



See Calendar.add(). It should do exactly what you need like Senthil's example illustrates.

Layne
 
vasu maj
Ranch Hand
Posts: 398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
uffff.. either I am speaking a foreign language or not speaking sense at all.
 
Senthil B Kumar
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey dude,
since one date is 24 Hrs behind your system date, i had suggested the -ve addition of the day. Since 24 hrs is 1 day and you can safely add -1 ,

are you getting it ?

or is your requirement different ?
 
vasu maj
Ranch Hand
Posts: 398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need two dates ( java.util.Date objects). One is the system date and the other is a date which is 24 hrs behind. How do I get them? Thanks Senthil but how do I get the dates from Calender class?

Thanks,

Vasu
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basic approach:

Get the system date. (a new java.util.Date object should default to this.
Call Calendar.getInstance(). (Do not cast to GregorianCalandar. There is no need).
Call .setTime() on your calendar instance. (This should default to the system time, but to make use that one is exactly 24 hours behind the other, make sure that you are at the same starting point.

call .add() on you calendar instance. Add either -1 days or -24 hours. (You are probably safest adding the -24 hours, as that is the specific requirement)

now call .getTime() on your calendar instance to get the Date object with the 24-hours-ago time.

Anything more detailed and I'm writing the code for you....

Take a look of the Calendar documentation to familiarize yourself with these methods. Calendar is a powerful class, but is one that people don't use frequently.
 
reply
    Bookmark Topic Watch Topic
  • New Topic