save Calendar in file

Posted on 2005-04-19
Last Modified: 2012-05-05
I am creating employee objects.  The requirement is
to save an employee record date info a  Calendar field.

No, problem.  Works great.

But how do I save it to a file?  None of the getInstance methods
seem to have all the args required to recreate an identical
Calendar object.

For now I just pulling out the time and saving that.

How do you do it?
Question by:hank1
    LVL 92

    Expert Comment

    To create a Calendar instance from a Date instance you would use:

    Calendar cal = Calendar.getInstance();
    LVL 1

    Author Comment


    long calTime = calender.getTime()

    write/read calTime to file

    Calendar cal = Calendar.getInstance();
    Date date = new Date(calTime);

    Now  have an EXACT copy of the original Calendar object...... right?

    Doesn't sound right.  what if they spin this up in India?  Don't you have
    to set some local things?
    LVL 92

    Accepted Solution

    Timezone details are associated with the Calendar instance, so if its run in another timezone the date will be adjusted accordingly.
    LVL 1

    Author Comment

    So, it will show that the record was created at, say,
    5am, Monday April 24, 2005, no matter where you open it
    and use it?  Seems this would work.  Time is time assuming
    the time you use is gmt.

    LVL 92

    Expert Comment

    No, the time displayed (if using the Calendar) would be the time in that timezone.
    You are storing the time without timezone (effectively GMT)
    LVL 15

    Expert Comment

    >> long calTime = calender.getTime()
    This returns java.util.Date and not time in milliseconds, I assume you meant calender.getTimeInMillis() or calendar.getTime().getTime()
    saving calTime to a file will not be good if you move that value to a different timezone because calTime will
    contain "number of milliseconds since January 1, 1970, 00:00:00 GMT" which will include both your timezone offset
    as well as daylight saving offset. you probably want to save that value - timezone offset and and - daylight saving factor
    which is basically converting your time into GMT time. When you applicatin deals with multiple timezone it is always good to store
    your time in GMT (and that applies also to the date stored in your DB).

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    This video teaches viewers about errors in exception handling.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now