Article Directory
Date
Also located under the java.util package.
In java, the easiest way to get the time is to instantiate the Date class directly.
In a custom format, take the current time-date:
Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
System.out.println(dateFormat.format(date));
Previously, the Date class could be used to obtain the structural information of the current date and time, for example:
Date date = new Date();
System.out.println(date.toLocaleString());
System.out.println(date.getDay()); // 当前星期几
System.out.println(date.getYear()); // 自从1900之后,经过了多少年
System.out.println(date.getMonth()); //当年第几个月,从0开始
System.out.println(date.getDate()); // 当前日
System.out.println(date.getSeconds()); // 当前秒
System.out.println(date.getMinutes()); // 当前分钟
System.out.println(date.getHours()); // 当前小时
However, the above APIs are all abandoned APIs. According to the description, it is possible that they have been abandoned since 1.1.
Calendar
The Calendar class is located under the java.util package and is a relatively new way of obtaining time in Java7.
is designed to convert dates and times between specific moments and calendar fields.
The more commonly used methods such as new Date().getHours()
etc. are already obsolete methods. The official document clearly recommends using the Calendar class instead.
Take the year, month, day, hour, minute and second respectively:
Calendar cal=Calendar.getInstance();
int y=cal.get(Calendar.YEAR);
int m=cal.get(Calendar.MONTH);
int d=cal.get(Calendar.DATE);
int h=cal.get(Calendar.HOUR_OF_DAY);
int mi=cal.get(Calendar.MINUTE);
int s=cal.get(Calendar.SECOND);
System.out.println("现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒");
In a custom format, take the current time-date:
Calendar calendar= Calendar.getInstance();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
System.out.println(dateFormat.format(calendar.getTime()));
Or directly sout the following:
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(Calendar.getInstance().getTime())
Note that the hh in the format is a 12-hour format. If you want to see a 24-hour format, use HH
Date/Time API
Java8 provides a brand new API to replace Java.util.Date
and Java.util.Calendar
.
It provides multiple classes to facilitate our manipulation of time, including:
- LocalDate
- LocalTime
- LocalDateTime
- ZonedDateTime
LocalDate
Located under the java.time package.
Only the date is displayed, not the time, you can use format to control the format.
Print the current date:
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(formatter.format(date)); // 2023-01-16
LocalTime
Also located under the java.time package.
Contrary to LocalDate, LocalTime can only get the time, not the date.
Print the current time:
LocalTime time = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
System.out.println(formatter.format(time)); // 15:42:19
LocalDateTime
At the same time, the date and time are obtained, and the usage is basically the same.
Print the current date and time:
LocalDateTime dt = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(formatter.format(dt)); // 2023-01-16 15:46:48
ZonedDateTime
Get structured fields for time and date
After ZonedDateTime ZonedDateTime = ZonedDateTime.now();
creation, the instance's date and time fields can be accessed via:
Calculation method of date and time:
Function
get standard time
The timestamp of standard time can System.currentTimeMillis()
be obtained by , it is said that this value is a pure value that is not affected by the time zone. Temporarily doubtful, because I have not verified it here.
like:1673861700672
It is also possible to convert the timestamp into a normal form that we can understand, but the system will automatically add the time zone during this process:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));