Home arrow static arrow Java Programming [Archive] - GregorianCalendar to int problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - GregorianCalendar to int problem
This topic has 7 replies on 1 page.

Posts:40
Registered: 5/27/04
GregorianCalendar to int problem  
Jul 16, 2004 8:36 AM



 
Hi all,

I am trying to save a GregorianCalendar object to a text file so I can set a shelf life for data being stored.

Currently I am setting the current date by creating a new GregorianCalendar object and write it to file by using "GregorianCalendarObject".get(Calendar.DATE). This works fine.

When I read the data in from the text file, all hell breaks loose.

I use Integer.parseInt() to convert the text file entry to three int's, then create an object from those int's. Like this:

// place tokens from text file into three int sectionsday = Integer.parseInt(st.nextToken());month = Integer.parseInt(st.nextToken());year = Integer.parseInt(st.nextToken()); // make calendar object from three integersdatePart = new GregorianCalendar(day, month, year);


When I write the datePart (GregorianCalendar object) back to the text file by using datePart.get(Calendar.DATE), etc, I don't get the origonal integers I wrote t othe text file.

Any ideas whats going wrong?
 

Posts:11,200
Registered: 7/22/99
Re: GregorianCalendar to int problem  
Jul 16, 2004 8:53 AM (reply 1 of 7)



 
First, the parameters are in order year-month-date. You are passing date-month-year.

Second, the "month" parameter is supposed to be one of Calendar's month constants, which happen to be 0-based (Calendar.JANUARY is 0), so the parameters you need to pass are (day, month-1, year).
 

Posts:11,200
Registered: 7/22/99
Re: GregorianCalendar to int problem  
Jul 16, 2004 8:54 AM (reply 2 of 7)



 
err.. the parameters you need to pass are (year, month-1, day)
 

Posts:40
Registered: 5/27/04
Re: GregorianCalendar to int problem  
Jul 16, 2004 9:01 AM (reply 3 of 7)



 
Aha!

That sounds like good news. Thanks for the help.

David.
 

Posts:6,750
Registered: 1/25/04
Re: GregorianCalendar to int problem  
Jul 16, 2004 9:16 AM (reply 4 of 7)



 
Seems like it would be easier to just parse the string with a DateFormat object.
 

Posts:40
Registered: 5/27/04
Re: GregorianCalendar to int problem  
Jul 16, 2004 9:32 AM (reply 5 of 7)



 
DateFormat object? I've looked into that in a small way. What's your idea?
 

Posts:13,769
Registered: 00-11-29
Re: GregorianCalendar to int problem  
Jul 16, 2004 9:34 AM (reply 6 of 7)



 
Why don't you just save the milliseconds value? I mean that's not really saving the Gregorian Calendar but it will save the date it represents perfectly.
 

Posts:11,200
Registered: 7/22/99
Re: GregorianCalendar to int problem  
Jul 16, 2004 9:44 AM (reply 7 of 7)



 
DateFormat object? I've looked into that in a small
way. What's your idea?

Since what you have is the date in a string format, you can easily parse it to a Date object. You can then set a calendar's time to that date object. This is probably easier than what you are doing now.
SimpleDateFormat parser = new SimpleDateFormat("d-M-YYYY");Date theDate = parser.parse("16-7-2004"); GregorianCalendar calendar = new GregorianCalendar();calendar.setTime(theDate);
See also:
http://java.sun.com/docs/books/tutorial/i18n/format/dateintro.html
http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
 
This topic has 7 replies on 1 page.