According to my task, I need to save the object of type Date to a file, and then restore this object from the file. Serialization can not be used.

So here. Such a thing does not work: new Date(date.toString()) . Then in what format should the date be saved so that it would be convenient to read it later?

  • It is more correct to save the date in ISO 8601 format. - Nofate
  • For data transfer between components, it’s really best to use ISO 8601, which provides both human and machine-readable date views. With ISO8601, however, Java before the eighth version has some problems, so I attach a link to a similar English-language question. - etki
  • one
    In addition, if you do not need to remember the time zone, it is convenient to drag the date in the form of a long ( new Date(date.getTime()) ). - Nofate
  • Ps. In the real world, you need to forget java.util.Date and use Joda Time (before Java 8) or java.time (after Java 8). - Nofate
  • 2
    As far as I remember, there is no time zone information in date, so here it’s easy to save a timestamp - Artem Konovalov

1 answer 1

We take date.getTime() we receive long which is UNIX Time , after we restore as new Date(unix) where unix is that number.