Java shares two ways to get the current date and time

  • 2020-04-01 03:11:16
  • OfStack

There are two ways:
Method 1: use java.util.Date class to achieve, and combined with java.text.DateFormat class to achieve the time format, see the following code:

import java.util.*;
import java.text.*;
//The following default time and date display is in Chinese language
//Generally, Chinese is the default language, and the format of time and date is MEDIUM style, such as: 2008-6-16 20:54:53
//The Date times shown below are based on the Date class and can be implemented using the Calendar class as shown in class testdate2.java
public class TestDate {
   public static void main(String[] args) {
      Date now = new Date();
      Calendar cal = Calendar.getInstance();

      DateFormat d1 = DateFormat.getDateInstance(); //MEDIUM style in the default language (Chinese), e.g., 2008-6-16 20:54:53
      String str1 = d1.format(now);
      DateFormat d2 = DateFormat.getDateTimeInstance();
      String str2 = d2.format(now);
      DateFormat d3 = DateFormat.getTimeInstance();
      String str3 = d3.format(now);
      DateFormat d4 = DateFormat.getInstance(); //Use the SHORT style to display the date and time
      String str4 = d4.format(now);
      DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //Display date, week, time (accurate to seconds)
      String str5 = d5.format(now);
      DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //Show the date. Time (accurate to seconds)
      String str6 = d6.format(now);
      DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //Display date, time (accurate to minute)
      String str7 = d7.format(now);
      DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //Display date, time (accurate to minute)
      String str8 = d8.format(now);//This is best compared to the SHORT style
      System.out.println(" with Date Mode display time : " + now);//This method displays the results and calendar.getinstance ().gettime ()
      System.out.println(" with DateFormat.getDateInstance() After formatting time: " + str1);
      System.out.println(" with DateFormat.getDateTimeInstance() After formatting time: " + str2);
      System.out.println(" with DateFormat.getTimeInstance() After formatting time: " + str3);
      System.out.println(" with DateFormat.getInstance() After formatting time: " + str4);

      System.out.println(" with DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL) After formatting time: " + str5);
      System.out.println(" with DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG) After formatting time: " + str6);
      System.out.println(" with DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT) After formatting time: " + str7);
      System.out.println(" with DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM) After formatting time: " + str8);
   }
}

Operation results:

 with Date Mode display time : Mon Jun 16 20:54:53 CST 2008
 with DateFormat.getDateInstance() After formatting time: 2008-6-16
 with DateFormat.getDateTimeInstance() After formatting time: 2008-6-16 20:54:53
 with DateFormat.getTimeInstance() After formatting time: 20:54:53
 with DateFormat.getInstance() After formatting time: 08-6-16  In the afternoon 8:54
 with DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL) After formatting time: 2008 years 6 month 16 day   Monday   In the afternoon 08 when 54 points 53 seconds  CST
 with DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG) After formatting time: 2008 years 6 month 16 day   In the afternoon 08 when 54 points 53 seconds 
 with DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT) After formatting time: 08-6-16  In the afternoon 8:54
 with DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM) After formatting time: 2008-6-16 20:54:53

Method 2: use the java.util.calendar class to implement, see the following:

import java.util.*;
import java.text.*;
//Here's how to implement Date times using the Calendar class, which is simpler than the Date class
public class TestDate2 {
   public static void main(String[] args) {

      Calendar ca = Calendar.getInstance();
      int year = ca.get(Calendar.YEAR);//For years
      int month=ca.get(Calendar.MONTH);//To get in
      int day=ca.get(Calendar.DATE);//To obtain,
      int minute=ca.get(Calendar.MINUTE);//points
      int hour=ca.get(Calendar.HOUR);//hours
      int second=ca.get(Calendar.SECOND);//seconds
      int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);

      System.out.println(" with Calendar.getInstance().getTime() Mode display time : " + ca.getTime());
      System.out.println(" with Calendar The date of acquisition is: " + year +" years "+ month +" month "+ day + " day ");

      System.out.println(" with Calendar The acquisition time is: " + hour +" when "+ minute +"points"+ second +"seconds");
      System.out.println(WeekOfYear);//Shows what day of the week it is today (the example I did happened to be on Tuesday, so the result shows 2, if you run again on week 6, it shows 6)
   }
}

The running result is:

 with Calendar.getInstance().getTime() Mode display time : Mon Jun 16 21:54:21 CST 2008
 with Calendar The date of acquisition is: 2008 years 5 month 16 day 
 with Calendar The acquisition time is: 9 when 54 points 21 seconds 

Summary: in the middle, method 2 is the most convenient, method 1 appears cent clumsy, but see individual like.
There's another way to use it

System.currentTimeMillis()

Related articles: