Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

typecast gregoriancalendar object as date object?

 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :-)
 
meenakshi ashokkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time and input.
Works out to about the same number of lines of codes ;-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic