Search code examples
javaobjecttype-conversioncmisopencmis

Reconvert java.util.GregorianCalendar.toString() back to GregorianCalendar


I am working with CMIS and Java, i loaded all properties from a document and wrote them into an xml file.

I use Object.toString() method to create text nodes in xml file. Therefore i used .toString() method to stringify GregorianCalendar method and it created an output like this

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=?,YEAR=2008,MONTH=7,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=18,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=?,HOUR=?,HOUR_OF_DAY=12,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=?,DST_OFFSET=?]

I need to parse this string back to java.util.GregorianCalendar Object.

Can you help me to solve this big problem?

Thank you.


Solution

  • If you can modify the node representation, I'd suggest a much easier way: use

    Calendar.getTimeInMillis()
    

    instead of toString(). It will give you back the timestamp. Then, to convert it back, you can use:

    Calendar.getInstance().setTimeInMillis(Long.parseLong(yourMillis))