Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

typecast gregoriancalendar object as date object?  RSS feed

 
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 :-)
 
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 ;-)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!