Use standard date formatting procedures

 DateFormat.getDateTimeInstance() allows us to
get the standard date formatter in several different ways. In the example below, we get four built-in date
formatters . They include a short, medium , long, and full date formats.

import java.text.DateFormat;
import java.util.Date;

public class DateExample4 {

public static void main(String[] args) {
Date date = new Date();

DateFormat shortDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.SHORT,
DateFormat.SHORT);

DateFormat mediumDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.MEDIUM,
DateFormat.MEDIUM);

DateFormat longDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.LONG,
DateFormat.LONG);

DateFormat fullDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.FULL);

System.out.println(shortDateFormat.format(date));
System.out.println(mediumDateFormat.format(date));
System.out.println(longDateFormat.format(date));
System.out.println(fullDateFormat.format(date));
}
}

 

Note that we pass two values ​​in each call to getDateTimeInstance. The first parameter
is the date style, and the second parameter is the time style. They are both primitive data types int (integer). Consider
readability For properties, we used the constants provided by the DateFormat class: SHORT, MEDIUM, LONG, and FULL. For more methods and options for obtaining time and date formatting procedures, see the explanation on the Sun Corporation Web site. 

When running our example program, it will output the following to standard output:
9/29/01 8:44 PM
Sep 29, 2001 8:44:45 PM
September 29, 2001 8:44:45 PM EDT
Saturday , September 29, 2001 8:44:45 PM EDT 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325818480&siteId=291194637