File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calendar variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar variables" Watch "Calendar variables" New topic
Author

Calendar variables

Carlos Delgado
Greenhorn

Joined: Aug 17, 2004
Posts: 15
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

Joined: Oct 29, 2001
Posts: 8907
    
    8

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Carlos Delgado
Greenhorn

Joined: Aug 17, 2004
Posts: 15
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calendar variables