Die im java.text-Paket vorhandene DateFormat-Klasse ist eine abstrakte Klasse, die zum Formatieren und Analysieren von Datumsangaben für jedes Gebietsschema verwendet wird. Es ermöglicht uns, Datum in Text zu formatieren und Text in Datum zu analysieren. Die DateFormat-Klasse bietet viele Funktionen zum Abrufen, Formatieren und Analysieren von Standarddatum und -uhrzeit. Die DateFormat-Klasse erweitert die Format-Klasse, was bedeutet, dass sie eine Unterklasse der Format-Klasse ist. Da es sich bei der DateFormat-Klasse um eine abstrakte Klasse handelt, kann sie für Unterklassen zur Datums-/Uhrzeitformatierung verwendet werden, die Datums- und Uhrzeitangaben sprachunabhängig formatieren und analysieren.
Der Format() Die Methode der DateFormat-Klasse in Java wird verwendet, um ein bestimmtes Datum in eine Datums-/Uhrzeitzeichenfolge zu formatieren. Grundsätzlich wird die Methode verwendet, um dieses Datum und diese Uhrzeit in ein bestimmtes Format umzuwandeln, z. B. MM/TT/JJJJ.
Syntax:
public final String format(Date date)>
Parameter: Die Methode nimmt einen Parameter date vom Objekttyp Date an und verweist auf das Datum, dessen String-Ausgabe erzeugt werden soll.
Rückgabetyp: Gibt Datum oder Uhrzeit im Zeichenfolgenformat MM/TT/JJJJ zurück.
Speicheraustausch
Beispiel 1:
Java
e r Modellbeispiele
// Java Program to Illustrate format() Method> // of DateTime Class> // Importing required classes> import> java.text.*;> import> java.util.Calendar;> // Main class> // DateFormat_Demo> public> class> GFG {> >// Main driver method> >public> static> void> main(String[] args)> >{> >// Initializing the first formatter> >DateFormat DFormat = DateFormat.getDateInstance();> >// Initializing the calendar Object> >Calendar cal = Calendar.getInstance();> >// Displaying the actual date> >System.out.println(>'The original Date: '> >+ cal.getTime());> >// Converting date using format() method> >String curr_date = DFormat.format(cal.getTime());> >// Printing the formatted date> >System.out.println(>'Formatted Date: '> + curr_date);> >}> }> |
Konvertieren Sie char in einen String
>
>Ausgabe:
The original Date: Wed Mar 27 11:12:29 UTC 2019 Formatted Date: Mar 27, 2019>
Beispiel 2:
Scanner Java
Java
Wie viele MB sind in einem GB?
// Java Program to Illustrate format() Method> // of DateTime Class> // Importing required classes> import> java.text.*;> import> java.util.*;> // Main class> // DateFormat_Demo> public> class> GFG {> >// Main driver method> >public> static> void> main(String[] args)> >{> >// Initializing the first formatter> >DateFormat DFormat = DateFormat.getDateTimeInstance(> >DateFormat.LONG, DateFormat.LONG,> >Locale.getDefault());> >// Initializing the calendar Object> >Calendar cal = Calendar.getInstance();> >// Displaying the actual date> >System.out.println(>'The original Date: '> >+ cal.getTime());> >// Converting date using format() method and> >// storing date in a string> >String curr_date = DFormat.format(cal.getTime());> >// Printing the formatted date on console> >System.out.println(>'Formatted Date: '> + curr_date);> >}> }> |
>
>Ausgabe
The original Date: Tue Jan 11 05:42:29 UTC 2022 Formatted Date: January 11, 2022 at 5:42:29 AM UTC>