Class for working with date in Java?

Can't choose, than to use Date or Calendar (or maybe something else?). The reason for this is that I haven't worked with one of them.


The problem is this. The user performs some action. They are recorded in the log. The magazine is not large, so it is the role of an ArrayList with objects (records). It is possible to fix with accuracy to the minute, but it is better to the second. After the application is closed, data is saved to a file, as XML. There and kept the whole magazine. Then when the application is loaded from file, all is restored. Don't ask me why XML.


Question for you, what should I use?
October 8th 19 at 03:48
4 answers
October 8th 19 at 03:50
Well, everything is simple. If you just store the date — Date enough. If you need to carry out various activities (e.g. counting the number of days between events etc) — then perhaps it makes sense and Сalendar to use.
Counting sort is not necessary. Thanks, went to read the Doc for Date. - Neha11 commented on October 8th 19 at 03:53
October 8th 19 at 03:52
If you're interested you can look at joda time
October 8th 19 at 03:54
As already advised above — Joda. But there is another alternative — time4j.
Tip from myself — keep the date always in the same time zone, and only in the derivation of transfer you need: stackoverflow.com/questions/508019/jpa-hibernate-store-date-in-utc-time-zone
October 8th 19 at 03:56
In Apache Commons DateUtils has many convenient wrappers to Date. Also look at the class TimeUnit, for example, need to in minutes the difference between two dates:

Date currentDate = new Date();
Date pastDate = DateUtils.addDays(currentDate, -7); //minus 7 days from current date
int min = TimeUnit.MILLISECONDS.toMinutes(currentDate.getTime()-pastDate.getTime());

Find more questions by tags Work with date / timeLoggingJavaCalendar