business scenarios, there is no lack of need to get the current system time to do some judgment, such as judging how long it takes for an execution process, and then record the time to return to the business to view; or the need for or a unique value to do some of the form form number, then the current system time is the only one that can be applied, and so on. That is summarized below which are several ways to get.
First, the System class currentTimeMillis () method
method function:Returns the current time in milliseconds from midnight UTC on January 1, 1970. The return type is long, indicating the current time in milliseconds.
Special Note: If you are trying to get a timestamp, it is recommended to use (), which is the most efficient way to get a timestamp, and the Date class can also get a timestamp, which is less efficient.
-
@Test
-
public void test(){
-
long l = (); // Getting timestamps is the most efficient
-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
String format = (l);
-
(l); //1663989713565
-
(format);//2022-09-24
-
}
II. AdoptionDate classto get the current time
-
@Test
-
public void test(){
-
Date date = new Date();
-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
String format = (date);
-
(format); //2022-09-24 11:28:22
-
(date); //Sat Sep 24 11:28:22 CST 2022
-
String year = ("%tY", date);
-
String month = ("%tm", date);
-
String day = ("%te", date);
-
("Today is:"+year+"-"+month+"-"+day); // Today is: 2022-09-24
-
}
Third, through the Calendar class to get the current time
-
@Test
-
public void test(){
-
Calendar instance = ();
-
(()); //1663990917312
-
(()); //Sat Sep 24 11:41:57 CST 2022
-
(()); // 2022
-
(()+1); // 9
-
(()); // 24
-
((Calendar.HOUR_OF_DAY)); //11
-
(()); //41
-
(()); //57
-
}
IV. LocalDate
-
LocalDate curCycleStartDate = ().with();
-
LocalDate curCycleEndDate = (6);
-
LocalDate preCycleStartDate = (7);
-
LocalDate preCycleEndDate = (1);
-
-
-
(()); //System current day
-
(curCycleStartDate); // This Monday
-
(curCycleEndDate); // this Monday + 1 day this Sunday
-
(preCycleStartDate); // This Monday - 7 days Last Monday
-
(preCycleEndDate); //Monday - 1 day Last Sunday
-
-
DateTimeFormatter dateTimeFormatter = ("yyyy-MM-dd hh:mm:ss");
-
((())); //System current day with hours, minutes and seconds
-
(().format(("yyyy-MM-dd hh:mm:ss")));
-
2023-06-01
-
2023-05-29
-
2023-06-04
-
2023-05-22
-
2023-05-28
-
2023-06-01 09:09:53
-
2023-06-01 09:09:53