• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar variables

 
Carlos Delgado
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all again!
I began to make a program for a library that uses Date variables. I found out that the Date type is being deprecated as my compiler says. Si I'm updating it to Calendar.
But I have a few questions:
1. Can I translate a String to a Calendar?
2. Can I know the difference (in days) between to Calendar variables?
3. Can I display a Calendar as "07/09/04" instead of "Thu Aug 19 18:07:00 CEST 2004"?

As an example, if I rent a book on 29th, August 2004 and today is 4th, September 2004... is there a way to get 6 days? In all operations I'd like to bypass the time, only working with the date.

Thanks as always!
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carlos Delgado:
Hi all again!
1. Can I translate a String to a Calendar?
3. Can I display a Calendar as "07/09/04" instead of "Thu Aug 19 18:07:00 CEST 2004"?


Those functions are covered by java.text.DateFormat and the subclass java.text.SimpleDateFormat
There's examples in the
Formatting chapter of the Java Tutorial.


2. Can I know the difference (in days) between to Calendar variables?


That work you'll have to do. Calendar has a getTimeInMillis() method. You can use it to find the difference in milliseconds between your two dates. Divide the difference by the number of milliseconds in a day and you have it.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. Can I know the difference (in days) between to Calendar variables?

Take a look at the Comparing Dates section of our Java Dates FAQ. You might also like to take a look at a couple of the other sections in that FAQ.
 
Carlos Delgado
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies!
But is not there an easier method for comparing dates? With the Date type variables it's possible to do it easy, but working with Calendar I think it's a step back to calculate it in miliseconds...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic