This blog summarizes some of the main method java on the inside to get the current time
System.currentTimeMillis()
Obtaining standard time may be () method by System.currentTimeMillis, when this method is not impact zone, the result is the timestamp format. E.g:
1543105352845
We can be converted into a format timestamp our easy to understand
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
Corresponding to the time stamp:
2018-11-25 at 01:22:12 CET
It is worth noting that this method returns a value based on our current system time, because the time zone around the world is not the same.
java.util.Date
In Java, get one of the current date The easiest way is to directly instantiate the Date class in the Java package java.util of.
Date date = new Date(); // this object contains the current date value
Above the acquired date can be format into the format we need, for example:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println(formatter.format(date));
Calendar API
Calendar category, date and time devoted to the conversion between a specific time and calendar field.
Use Calendar to get the current date and time is very simple:
Calendar calendar = Calendar.getInstance(); // get current instance of the calendar
To date, we can very easily format the date into the format we need
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println(formatter.format(calendar.getTime()));
Result of the above code is printed as follows:
25-11-2018 00:43:39
Date/Time API
Java 8 provides a new API, to replace java.util.Date and java.util.Calendar. Date / Time API provides several classes to help us to complete the work, including:
Zhengzhou infertility hospital: http: //jbk.39.net/yiyuanzaixian/zztjyy/
LocalDate
LocalTime
localdateti to
ZonedDateTime
LocalDate
LocalDate just a date, there is no time. This means that we can only get the current date, but no specific time of day.
LocalDate date = LocalDate.now(); // get the current date
We can format it
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
System.out.println(date.format(formatter));
The results obtained only date, for example:
https://wenku.baidu.com/view/4bac53c8326c1eb91a37f111f18583d048640f38
25-11-2018
LocalTime
LocalTime and LocalDate contrary, it represents a time, no date. This means that we can only get the current time of day, rather than the actual date:
LocalTime time = LocalTime.now(); // get the current time
Format may be as follows
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
System.out.println(time.format(formatter));
The results obtained are similar to the following:
00:55:58
localdateti to
The last one is the LocalDateTime, is most commonly used in Java Date / Time classes representative of a combination of the former two classes - i.e., the date and time value:
LocalDateTime dateTime = LocalDateTime.now(); // get the current date and time
The format is the same way
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(dateTime.format(formatter));
Date obtained similar results:
25-11-2018 00:57:20