I
I
Ivan Karabadzhak2012-01-23 22:30:53
Java
Ivan Karabadzhak, 2012-01-23 22:30:53

Date class in Java?

Can't decide whether to use Date or Calendar (or maybe there's something else?). The reason for this is that I haven't worked with any of them yet.
The task is this. The user performs some action. They are logged. The log is not large, so its role is played by an ArrayList with objects (records). You can fix to the minute, but better to the second. After closing the application, the data is saved to a file, in the form of XML. The entire log is also stored there. Then, when the application is loaded, everything is restored from the file. Don't ask why XML.
Question for you, which is better to use?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
SSiarhei, 2012-01-23
@SSiarhei

Well, everything is simple here. If you just store the date, Date is enough. If you need to carry out various actions (for example, counting the number of days between events, etc.), then perhaps it makes sense to use Calendar.

D
Dmitry404, 2012-01-23
@Dmitry404

If interested, you can look at joda time

S
serso, 2012-01-24
@serso

As already advised above - Joda. But there is another alternative - time4j .
Advice from myself - always store the date in the same time zone, and only translate it into the right one when outputting: stackoverflow.com/questions/508019/jpa-hibernate-store-date-in-utc-time-zone

L
Lihonosov, 2012-01-24
@Lihonosov

Apache Commons DateUtils has many convenient wrappers for working with Date. Also look at the TimeUnit class , for example, you need to get the difference between two dates in minutes:

Date currentDate = new Date();
Date pastDate = DateUtils.addDays(currentDate, -7); //минус 7 дней от текущей даты
int min = TimeUnit.MILLISECONDS.toMinutes(currentDate.getTime()-pastDate.getTime());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question