How to get the current date and time in Java: the application of the date and the Calendar class

How to get the current date and time in Java: the application of the date and the Calendar class In Java programming, the current date and time often need to be obtained.Java provides two commonly used classes to handle dates and time: date and Calendar class.This article will introduce how to use these two classes to obtain the current date and time, and provide some Java code examples. 1. Use the date class to get the current date and time The Date class is the most basic date and time in Java. It represents a specific moment, including the date and time information.To obtain the current date and time, you can use the DATE class non -constructor function to create a DATE object that represents the current time, and then format it into the required string format. The following is an example of Java code for obtaining the current date and time: import java.util.Date; import java.text.SimpleDateFormat; public class GetCurrentDateTime { public static void main(String[] args) { // Create the Date object that represents the current time Date now = new Date(); // Create the SimpleDateFormat object and specify the format of the date and time SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Formatting Date object is a string String currentDateTime = sdf.format(now); // Output the current date and time System.out.println ("current date and time:" + Currentdatetime); } } In the above code, the DATE classless creation function first created a DATE object that indicates the current time.Then, a SimpleDateFormat object was created, and the format of the specified date time was "yyyy-MM-DD HH: MM: SS".Express the hour, "mm" means minute, "SS" means seconds.Next, by calling the format () method of the SimpleDateFormat object, the date object is formatted into a string and output the formatted string. 2. Use the Calendar class to get the current date and time The Calendar class is an abstract base for processing date and time, which provides various methods to operate the date and time.With the use of the Calendar class to obtain more flexibility in the current date and time, you can get the values of all parts such as the year, month, day, time, time, minute, second. The following are examples of Java code for the current date and time using the Calendar class: import java.util.Calendar; import java.util.Date; import java.text.SimpleDateFormat; public class GetCurrentDateTime { public static void main(String[] args) { // Create a CALENDAR object Calendar calendar = Calendar.getInstance(); // Get the current time Date now = calendar.getTime(); // Create the SimpleDateFormat object and specify the format of the date and time SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Formatting Date object is a string String currentDateTime = sdf.format(now); // Output the current date and time System.out.println ("current date and time:" + Currentdatetime); } } In the above code, first call the Getinstance () method of the Calendar class to create a Calendar object.Then, get the current time by calling the Gettime () method of the Calendar object, and return a date object.Next, the DATE object is formatted into a string with the above method as the above method, and the use of the SIMPLEDATEFORMAT class is the same as the above method. Summarize This article introduces how to obtain the current date and time in Java, and provides example code using Date and Calendar classes.The method of using the DATE class is simple and intuitive, suitable for general date and time operation; the method of using the Calendar class is more flexible, and it can perform more accurate date and time calculation.According to specific needs, choose the appropriate class and methods to obtain the current date and time.