Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java date beginner question

 
krillian konstatos
Ranch Hand
Posts: 49
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calendar has a method called add. With this you can increment the calendar for seconds, minutes, days, etc.
 
krillian konstatos
Ranch Hand
Posts: 49
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic