How can the current date be output and formatted?

Up to Java 7, the classes Dateand represent GregorianCalendartwo options for displaying the current date. Formatting is done by the classes DateFormatand their derivation SimpleDateFormat. Starting with Java 8, the API has been changed considerably and made more intuitive overall.

Up to Java 7

In the example, two methods are declared that give a little insight into the topic. the first method printGregorianCalendarDate()demonstrates the procedure with the help of the class GregorianCalendar . It represents a concretion of the abstract class Calendar , which offers very extensive possibilities to manipulate time and date fields. For further information, reference must be made to the Java documentation at this point due to the huge range of options, which also lists a few examples for a change , especially in the GregorianCalendar chapter .

In the method below, a GregorianCalendar object is first generated by calling the parameterless constructor, which represents the current time in the format of the system-wide country setting. In order to get adequate formatting, the class is DateFormatused, which has a number of static, sometimes overloaded methods to get the formatters of the individual time and date fields. Static int constants can be passed to these methods, which determine the parts and the exact formatting of the output. However, the basis is always the formatting specified by the country settings and the time zone.

The SimpleDateFormat class is even more flexible, with the help of which, among other things, date strings can be formatted according to your own requirements. As shown in the example in the method printSimpleDateFormat() , a string is passed to the constructor, which determines the exact formatting with the help of letter patterns. All patterns are listed in the class documentation SimpleDateFormat . 
Its method is format()called on the SimpleDateFormatter object, to which an object of the type is Datepassed as a parameter .

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

public class 

    CurrentDate { static void printGregorianCalendarDate () { 
        GregorianCalendar now = new GregorianCalendar (); 
        DateFormat df = DateFormat.getDateInstance (DateFormat.SHORT); // 04/14/12 
        df = DateFormat.getDateInstance (DateFormat.MEDIUM); // 04/14/2012 
        df = DateFormat.getDateInstance (DateFormat.LONG); // April 14, 2012 
        System.out.println (df.format (now.getTime ())); 
        df = DateFormat.getTimeInstance (DateFormat.SHORT); // 21:21 
        df = DateFormat.getTimeInstance (DateFormat.MEDIUM); // 21:21:12 
        df = DateFormat.getTimeInstance (DateFormat.LONG); // 21:21:12 CEST 
        System.out.println (df.format (now.getTime ())); 
        df = DateFormat.getDateTimeInstance (DateFormat.SHORT, DateFormat.LONG); // 04/14/12 21:34:07 CEST 
        System.out.println (df.format (now.getTime ())); 
    } 

    static void printSimpleDateFormat () { 
        SimpleDateFormat formatter = new SimpleDateFormat ( 
                “yyyy.MM.dd – HH: mm: ss”); 
        Date currentTime = new Date (); 
        System.out.println (formatter.format (currentTime)); // 04/14/2012 – 21:34:07  
    } 

    public static void main (String [] args) { 
        printGregorianCalendarDate (); 
        printSimpleDateFormat (); 
    } 

From Java 8

v.8.0Starting with Java 8, the entire date-time API has been significantly redesigned. Concepts from the JODA library were adopted here. The core classes are now included in the package java.time , the classes for formatting the date and time in the package java.time.format.

The following example class demonstrates the formatting and output of the current date using the Java 8 API using two methods. 
formatDateTime()shows the formatting of an LocalDateTimeobject that is now()initialized with the current point in time by calling the static method . It is an object that contains the date and time combined in the ISO-8601form. 
The class DateTimeFormatteris responsible for the formatting. It includes a number of predefined static formatters, some of which are shown here. The method format()does the actual formatting. It is declared in the three classes LocalDateTime, LocalDateand LocalTimeand each expects a parameter of the type DateTimeFormatter.
The static method of ofPattern()the class must be mentioned separately here DateTimeFormatter. It accepts a string of predefined pattern characters with which the formatting can be precisely controlled. An overview of the characters available for this can be found in the API Docs of the classDateTimeFormatter  .

import java.time.LocalDateTime; 
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.LocalDate;

public class 

    CurrentDateJava8 {static void formatDateTime () { 
        LocalDateTime now = LocalDateTime.now (); 
        DateTimeFormatter df; 
        df = DateTimeFormatter.BASIC_ISO_DATE; // 20160131 
        df = DateTimeFormatter.ISO_LOCAL_DATE; // 2016-18-31 
        df = DateTimeFormatter.ISO_DATE_TIME; // 2016-01-31T20: 07: 07.095 
        System.out.println (now.format (df));        
        df = DateTimeFormatter.ofPattern (“dd.MM.yyyy kk: mm”); // 01/31/2016 8:07 p.m. 
        System.out.println (now.format (df));      
    } 
     
    static void formatDate () { 
        LocalDate date = LocalDate.now (); 
        DateTimeFormatter df; 
        System.out.println (date); // 2016-01-31 
        df = DateTimeFormatter.ofLocalizedDate (FormatStyle.FULL); // Sunday, January 31, 2016 
        df = DateTimeFormatter.ofLocalizedDate (FormatStyle.LONG); // January 31, 2016 
        df = DateTimeFormatter.ofLocalizedDate (FormatStyle.MEDIUM); // 01/31/2016 
        df = DateTimeFormatter.ofLocalizedDate (FormatStyle.SHORT); // 01/31/16 
        System.out.println (date.format (df)); 
         
    } 

    public static void main (String [] args) { 
        formatDateTime (); 
        formatDate (); 
    } 

formatDate()An object of the type is LocalDate created in the method , which contains a date without time specifications. Its formatting is done in a similar way to LocalDateTime. In the example, some formatting is FormatStyledemonstrated using the constants of the class . Here, too, the actual formatting takes place by calling the method format().

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

جميع الحقوق محفوظة لموقع كيفاش 2024