Looking at the Java date class, it's almost defected, is it better not to use it at all?

Asked 1 years ago, Updated 1 years ago, 63 views

Looking at the generators, we find the current time

Date date = new Date();

Other than that, everything was decided. Then you can just tell them to use Calendar. Why did they leave that?

java date

2022-09-22 13:05

3 Answers

It seems that the Date class itself is not intended to be precluded, but rather to have been precluded from generators and methods that are not suitable for multinational use.


2022-09-22 13:05

On Android, Date was almost Deferred after API1 To replace Date, the basic method of Date is almost It is provided by Calendar class.

The
int getDate() method is Calendar.get(Calendar.DATE)

The getDay() method is Calendar.get(Calendar.DAY_OF_WEEK)

The int getHours() method is Calendar.get(Calendar.HOUR_OF_DAY)

The int getMinutes() method is Calendar.get(Calendar.MINUTE)
The int getMonth() method is called Calendar.get(Calendar.MONTH)
Use the int getSeconds() method instead of Calendar.get(Calendar.SECOND).

You can also use Calendar's set method for the set~~() method.
void setDate() -> Calendar.set(Calendar.DATE, day)


2022-09-22 13:05

If you disable it just because it is defined, it will affect programs that have already been distributed and are being serviced.

Even if a new function appears in a new version, the existing one should be available.


2022-09-22 13:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.