jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes typecast gregoriancalendar object as date object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "typecast gregoriancalendar object as date object?" Watch "typecast gregoriancalendar object as date object?" New topic
Author

typecast gregoriancalendar object as date object?

achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi I need to do some simple date calculations, like find out the number of days between two dates e.g.

Obviously I can do this using Date objects.
But I have codes which use GregorianCalendar objects. The long (workable) way is to typecast the GregorianCalendar objects and then parse them back to Date objects for the calculation.
That seems rather cumbersome!
Is there a more elegant way of subtracting (or adding) two GregorianCalendar objects without to get the difference in number of days?
TIA :-)


humanum errare est.
meenakshi ashokkumar
Greenhorn

Joined: Mar 07, 2004
Posts: 22
hi,
check out if this kinda code can be of help!
import java.util.*;
class GregT{
public static void main(String ar[]){
Calendar gcala=new GregorianCalendar(2004,01,17,8,32,30);//Jan 17th 2004
Calendar gcaln=new GregorianCalendar(2004,03,17,8,32,30);//March 17th 2004
int var1=gcala.get(Calendar.DAY_OF_YEAR);
int var2=gcaln.get(Calendar.DAY_OF_YEAR);
System.out.println(var2-var1);

}
}
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Thanks for your time and input.
Works out to about the same number of lines of codes ;-)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: typecast gregoriancalendar object as date object?