Two Laptop Bag*
The moose likes Beginning Java and the fly likes java date beginner question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java date beginner question" Watch "java date beginner question" New topic
Author

java date beginner question

krillian konstatos
Ranch Hand

Joined: Jan 03, 2011
Posts: 49

Hello, java world!

I have a project in which i need to work with the date and time classes in java. This is where
i always fails. I have a calendar object that will represents dates only not hours minutes and
seconds. This date object will start from the year 1900 or rather using the date format
1900-01-01. I need to loop through time untill the current date. So far i have only managed
to parse dates in java not using the loop methods in the GregorianCalendar class that i
must use to accomplish the task in question. Do i have to use before or after methods in
the GregorianCalendar class in a while loop or both?




What i know i must do is to here after initialize a while loop that begins with the previous mentioned
startdate (1900-01-01) and then process a date object from the GregorianCalendar instantiation variable.
Sadly for me i have not gotten past this point. If any one here could point me in the correct direction
at how to loop through a GregorianCalendar object i would be very much thankful or post links to
examples that covers the issue in question.



//Regards,
K. Konstatos.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Calendar has a method called add. With this you can increment the calendar for seconds, minutes, days, etc.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
krillian konstatos
Ranch Hand

Joined: Jan 03, 2011
Posts: 49

Rob Spoor wrote:Calendar has a method called add. With this you can increment the calendar for seconds, minutes, days, etc.


Thank you for your tip i have tried that but i can't see how to use the add method correctly. I am thinking about the leap year issue
when looping through the calendar. So far i have tried the below listed code but i dont know if i am thinking in the right direction.



Very kind regards,
Krillian!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

If you just want to loop one day / month at a time, all you need to add is one single day . month. The calendar object will take care of leap years and everything.
krillian konstatos
Ranch Hand

Joined: Jan 03, 2011
Posts: 49

It is getting clearer up there by now but what about the looping part? i guess i have to loop through both the year,
month and day of month value and not just add(Calendar.YEAR, 1); and so on in a year loop ... maybe i should
do this using arrays of objects representing the year month and day ... pardon me if i make it sound more complex
than what it maybe is.

regards,
krillian!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

it is not clear to me what you are trying to do exactly - maybe because it is not clear to yourself.

So...

You seem to have created a date object that represents 1900-01-01. Now you say you "need to loop through time untill the current date"

do you need to progress 1 second at a time? a day at a time? 50 years at a time? or do you just need a date object with the current date/time?

basically, I would do something like this:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

krillian konstatos wrote:i guess i have to loop through both the year, month and day of month value and not just add(Calendar.YEAR, 1); and so on in a year loop ...

It looks to me that you want to add a single day to the current calendar, then add another day, and another... After adding 365 days (366 in leap years) you'll automatically have added a year.
krillian konstatos
Ranch Hand

Joined: Jan 03, 2011
Posts: 49

fred rosenberger wrote:




Thank you so very much for your kind answers i will proceed with further attempts at this until i get it right ...
I hope i have made things a little bit clearer by now ...



//Krillian
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36498
    
  16
Go and search for Apache Commons and Joda Time (or maybe Yoda Time??). You may find time and date classes there which are easier to use than the standard Java™ API.
krillian konstatos
Ranch Hand

Joined: Jan 03, 2011
Posts: 49

Campbell Ritchie wrote:Go and search for Apache Commons and Joda Time (or maybe Yoda Time??). You may find time and date classes there which are easier to use than the standard Java™ API.


Hi!

I have thought about something like this before but then again i want to avoid using external libraries i dont know that mush
about licensing issues and so on but thanks a lot for the tip!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java date beginner question
 
Similar Threads
Date Calculations - Difference between 2 dates is off
How to check for two dates equality in HQL ?
Date; Previous date, Calendar; Gregorian
date format question
SimpleDateFormat problem with 2-digit year