What you definitely should NOT do is store them in your own format. Save the Long value representing the Unix era.
DateTime is nothing more than a computer number. This number represents the number of seconds (or milliseconds) since 1970-01-01 00:00:00 UTC. This is beyond the scope of this answer to explain why this date was universally chosen, but you can find it by doing a search on Unix Epoch or reading http://en.wikipedia.org/wiki/Unix_time .
It also means the lack of time zone information stored in DateTime itself. It is important to keep this in mind when discussing dates and times. For things like comparing DateTime objects, nothing happens regarding localization or time zones. Only when formatting the time, which means enough to make it readable to people, or for operations such as getting the start of the day, do time zones take effect.
This is why you should not store time like 20:11:15 in a string-like format, because this information is meaningless without time zone information. I will give one example here: consider the moment when the clock moves back 1 hour, for example, when changing from summer time. This has happened in many countries. What is your line 02:30? First or second?
Calculations such as subtraction are as simple as with numbers. For example: Date newDate = new Date(date1.getTime() - date2.getTime()); . Or want to add an hour to the date? Date newDate = new Date(oldDate.getTime() + 1000 * 60 * 60);
If you need more sophisticated material, then using time in Joda would be a good idea, as already suggested. But it is entirely possible to do this even with native libraries.
If there is one resource that taught me a lot about date / time, that would be http://www.odi.ch/prog/design/datetime.php
Sebastiaan van den broek
source share