logo

Java SimpleDateFormat

Die Klasse java.text.SimpleDateFormat stellt Methoden zum Formatieren und Analysieren von Datum und Uhrzeit in Java bereit. Das SimpleDateFormat ist eine konkrete Klasse zum Formatieren und Analysieren von Datumsangaben, die die Klasse java.text.DateFormat erbt.

Beachte das Formatierung bedeutet, das Datum in eine Zeichenfolge umzuwandeln Und Parsen bedeutet, eine Zeichenfolge in ein Datum umzuwandeln .

Konstruktoren der Klasse SimpleDateFormat

SimpleDateFormat(String pattern_args): Instanziiert die SimpleDateFormat-Klasse mithilfe des bereitgestellten Musters – „pattern_args“, Standard-Datumsformatsymbole für das Standard-FORMAT-Gebietsschema.

SimpleDateFormat(String pattern_args, Locale locale_args): Instanziiert die SimpleDateFormat-Klasse mithilfe des bereitgestellten Musters – „pattern_args“. Für das bereitgestellte FORMAT-Gebietsschema lauten die Standardsymbole für das Datumsformat - locale_args.

SimpleDateFormat(String pattern_args, DateFormatSymbols formatSymbols): Instanziiert die SimpleDateFormat-Klasse und verwendet das bereitgestellte Muster – „pattern_args“ und die Datumsformatsymbole.

Java SimpleDateFormat-Beispiel: Datum zu String

Sehen wir uns das einfache Beispiel an Datum in Java formatieren Verwendung der Klasse java.text.SimpleDateFormat.

Dateiname: SimpleDateFormatExample.java

Java-erweiterte Schleife
 import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatExample { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat('dd/MM/yyyy'); String strDate= formatter.format(date); System.out.println(strDate); } } 
Testen Sie es jetzt

Ausgabe:

13/04/2015 

Hinweis: M (großes M) steht für den Monat und m (kleines m) für die Minute in Java.

Sehen wir uns das vollständige Beispiel an Datum und Uhrzeit in Java formatieren Verwendung der Klasse java.text.SimpleDateFormat.

Dateiname: SimpleDateFormatExample2.java

 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class SimpleDateFormatExample2 { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat('MM/dd/yyyy'); String strDate = formatter.format(date); System.out.println('Date Format with MM/dd/yyyy : '+strDate); formatter = new SimpleDateFormat('dd-M-yyyy hh:mm:ss'); strDate = formatter.format(date); System.out.println('Date Format with dd-M-yyyy hh:mm:ss : '+strDate); formatter = new SimpleDateFormat('dd MMMM yyyy'); strDate = formatter.format(date); System.out.println('Date Format with dd MMMM yyyy : '+strDate); formatter = new SimpleDateFormat('dd MMMM yyyy zzzz'); strDate = formatter.format(date); System.out.println('Date Format with dd MMMM yyyy zzzz : '+strDate); formatter = new SimpleDateFormat('E, dd MMM yyyy HH:mm:ss z'); strDate = formatter.format(date); System.out.println('Date Format with E, dd MMM yyyy HH:mm:ss z : '+strDate); } } 
Testen Sie es jetzt

Ausgabe:

Date Format with MM/dd/yyyy : 04/13/2015 Date Format with dd-M-yyyy hh:mm:ss : 13-4-2015 10:59:26 Date Format with dd MMMM yyyy : 13 April 2015 Date Format with dd MMMM yyyy zzzz : 13 April 2015 India Standard Time Date Format with E, dd MMM yyyy HH:mm:ss z : Mon, 13 Apr 2015 22:59:26 IST 

Java SimpleDateFormat-Beispiel: String bis Datum

Sehen wir uns das einfache Beispiel an String in Datum umwandeln Verwendung der Klasse java.text.SimpleDateFormat.

Dateiname: SimpleDateFormatExample3.java

 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatExample3 { public static void main(String[] args) { SimpleDateFormat formatter = new SimpleDateFormat('dd/MM/yyyy'); try { Date date = formatter.parse('31/03/2015'); System.out.println('Date is: '+date); } catch (ParseException e) {e.printStackTrace();} } } 
Testen Sie es jetzt

Ausgabe:

Date is: Tue Mar 31 00:00:00 IST 2015 

Methoden

set2DigitYearStart()

Syntax:

 public void set2DigitYearStart(Date startDate) 

Parameter:

startDate: Das Datum wurde im Bereich von startDate bis startDate + 100 Jahre festgelegt

Rückgabetyp:

Der Rückgabetyp der Methode ist void

Implementierung:

Mal sehen, wie man die Methode im Code implementieren kann.

Dateiname: Set2DigitYearStart.java

 // important import statements import java.util.Calendar; import java.text.*; public class Set2DigitYearStart { // main method public static void main(String argvs[]) throws InterruptedException { // creating an object of the class SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat('MM / dd / yy' ); try { // getting the instance of the class Calendar Calendar clndr = Calendar.getInstance(); clndr.setTime(sdf.parse('11 / 12 / 21')); System.out.println('Initial Timing is : ' + clndr.getTime()); // Setting 2020 // Using the set2DigitYearStart() method sdf.set2DigitYearStart(sdf.parse('02 / 02 / 2000')); clndr.setTime(sdf.parse('02 / 02 / 15')); System.out.println('The New Timing is : ' + clndr.getTime()); } catch (ParseException exp) { exp.printStackTrace(); } } } 

Ausgabe:

 Initial Timing is : Fri Nov 12 00:00:00 GMT 2021 The New Timing is : Mon Feb 02 00:00:00 GMT 2015 

get2DigitYearStart()

Syntax:

 public Date get2DigitYearStart() 

Parameter:

Für diese Methode ist kein Parameter erforderlich

Rückgabetyp:

Der Rückgabetyp der Methode ist „Datum“ und gibt den Beginn des 100-Jahres-Zeitraums zurück, der während der Analyse festgelegt wurde.

Implementierung:

Mal sehen, wie man die Methode im Code implementieren kann.

Dateiname: Get2DigitYearStart.java

 // important import statements import java.util.Calendar; import java.text.*; public class Get2DigitYearStart { // main method public static void main(String argvs[]) throws InterruptedException { // creating an object of the class SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat('MM / dd / yy' ); try { // getting the instance of the class Calendar Calendar clndr = Calendar.getInstance(); clndr.setTime(sdf.parse('11 / 12 / 21')); System.out.println('Initial Timing is : ' + clndr.getTime()); // Setting 2020 // Using the set2DigitYearStart() method sdf.set2DigitYearStart(sdf.parse('02 / 02 / 2000')); System.out.println('The New Timing is : ' + clndr.getTime()); // Using the method get2DigitYearStart() for checking the start year clndr.setTime(sdf.get2DigitYearStart()); System.out.println('The start year is: ' + clndr.get(Calendar.YEAR)); } catch (ParseException exp) { exp.printStackTrace(); } } } 

Ausgabe:

 Initial Timing is : Fri Nov 12 00:00:00 GMT 2021 The New Timing is : Mon Feb 02 00:00:00 GMT 2015 The start year is: 2000 

toPattern()

Syntax:

 public String toPattern() 

Parameter:

Für diese Methode ist kein Parameter erforderlich

Rückgabetyp:

Der Rückgabetyp der Methode ist String und gibt das Datumsformatmuster zurück.

Implementierung:

Mylivecricket-Alternative

Mal sehen, wie man die Methode im Code implementieren kann.

Dateiname: ToPattern.java

C++-String-Split
 // important import statements import java.util.Calendar; import java.text.*; public class ToPattern { // main method public static void main(String argvs[]) throws InterruptedException { // creating an object of the class SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat(); // Initializing the Calendar object Calendar clndr = Calendar.getInstance(); // getting the Current Date String currDate = sdf.format(clndr.getTime()); System.out.println('Current Date : ' + currDate); // Using the toPattern() method // Displaying the Date Pattern System.out.println('The Date Pattern is: ' + sdf.toPattern()); } } 

Ausgabe:

 Current Date : 12/11/21, 7:24 AM The Date Pattern is: M/d/yy, h:mm a 

parse()

Syntax:

 public Date parse() 

Parameter:

Für diese Methode ist kein Parameter erforderlich

Rückgabetyp:

Der Rückgabetyp der Methode ist Date und gibt das analysierte Datum zurück.

Implementierung:

Mal sehen, wie man die Methode im Code implementieren kann.

Dateiname: Parse.java

 // important import statements import java.util.Calendar; import java.text.*; public class Parse { // main method public static void main(String argvs[]) throws InterruptedException { // creating an object of the class SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat('MM / dd / yy' ); try { // getting the instance of the class Calendar Calendar clndr = Calendar.getInstance(); clndr.setTime(sdf.parse('11 / 12 / 21')); System.out.println('Initial Timing is : ' + clndr.getTime()); } catch (ParseException exp) { exp.printStackTrace(); } } } 

Ausgabe:

 Initial Timing is : Fri Nov 12 00:00:00 GMT 2021 

applyPattern()

Syntax:

 public void applyPattern() 

Parameter:

Für diese Methode ist kein Parameter erforderlich

Rückgabetyp:

Der Rückgabetyp der Methode ist void. Daher gibt die Methode nichts zurück.

Implementierung:

Mal sehen, wie man die Methode im Code implementieren kann.

Dateiname: ApplyPattern.java

 // important import statements import java.util.Calendar; import java.text.*; public class ApplyPattern { // main method public static void main(String argvs[]) throws InterruptedException { // creating an object of the class SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat(); // Initializing calendar Object Calendar clndr = Calendar.getInstance(); // Using the arg pattern String ar = 'dd / MM / yyyy HH:mm Z'; // Using the method applyPattern() to set the date to arg format sdf.applyPattern(ar); // for the current time and date String currDate = sdf.format(clndr.getTime()); System.out.println('The current date is: ' + currDate); // Printing the pattern used System.out.println('The Pattern applied is: ' + sdf.toPattern()); } } 

Ausgabe:

 The current date is: 11 / 12 / 2021 07:41 +0000 The Pattern applied is: dd / MM / yyyy HH:mm Z 

Format()

Syntax:

 public final String format(Date args) 

Parameter:

Die Methode verwendet Date als Argument

Iterieren Sie durch Map Java

Rückgabetyp:

Der Rückgabetyp der Methode ist String und die Methode gibt die formatierte Zeichenfolge des Datums zurück.

Implementierung:

Mal sehen, wie man die Methode im Code implementieren kann.

Dateiname: Format.java

 // important import statements import java.util.Calendar; import java.text.*; public class Format { // main method public static void main(String argvs[]) throws InterruptedException { // creating an object of the class SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat(); // Initializing calendar Object Calendar clndr = Calendar.getInstance(); System.out.println('The actual date is: ' + clndr.getTime()); // use theh format() method for the current time and date String currDate = sdf.format(clndr.getTime()); System.out.println('The formatted date is: ' + currDate); } } 

Ausgabe:

 The actual date is: Sat Dec 11 13:48:36 GMT 2021 The formatted date is: 12/11/21, 1:48 PM 

toLocalizedPattern()

Syntax:

 public String toLocalizedPattern() 

Parameter:

Die Methode akzeptiert kein Argument

Rückgabetyp:

Der Rückgabetyp der Methode ist String, und die Methode gibt die Datumsmusterzeichenfolge des Datumsformatierers zurück.

Implementierung:

Mal sehen, wie man die Methode im Code implementieren kann.

Dateiname: ZuLocalizedPattern.java

 // important import statements import java.util.Calendar; import java.text.*; public class ToLocalizedPattern { // main method public static void main(String argvs[]) throws InterruptedException { // creating an object of the class SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat(); // Initializing calendar Object Calendar clndr = Calendar.getInstance(); System.out.println('The Date is: ' + sdf.format(clndr.getTime())); // Using the format() method for formatting the Date to String System.out.println('The pattern in the DateFormater is : ' + sdf.toLocalizedPattern()); } } 

Ausgabe:

 The Date is: 12/11/21, 3:01 PM The pattern in the DateFormater is : M/d/yy, h:mm a