logo

Java SimpleDateFormat

La classe java.text.SimpleDateFormat fournit des méthodes pour formater et analyser la date et l'heure en Java. SimpleDateFormat est une classe concrète pour le formatage et l'analyse de la date qui hérite de la classe java.text.DateFormat.

Remarquerez que le formatage signifie convertir la date en chaîne et l'analyse signifie convertir la chaîne en date .

Constructeurs de la classe SimpleDateFormat

SimpleDateFormat (String pattern_args) : Instancie la classe SimpleDateFormat à l'aide du modèle fourni - pattern_args, symboles de format de date par défaut pour les paramètres régionaux FORMAT par défaut.

SimpleDateFormat (String pattern_args, Locale locale_args) : Instancie la classe SimpleDateFormat à l'aide du modèle fourni - pattern_args. Pour le FORMAT Locale fourni, les symboles de format de date par défaut sont - locale_args.

paramètres du navigateur Internet

SimpleDateFormat (String pattern_args, DateFormatSymbols formatSymbols) : Instancie la classe SimpleDateFormat et utilise le modèle fourni - pattern_args et le format de dateSymbols.

Exemple Java SimpleDateFormat : date en chaîne

Voyons l'exemple simple pour formater la date en Java en utilisant la classe java.text.SimpleDateFormat.

Nom de fichier: SimpleDateFormatExample.java

 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); } } 
Testez-le maintenant

Sortir:

13/04/2015 

Remarque : M (M majuscule) représente le mois et m (petit m) représente les minutes en Java.

Voyons l'exemple complet pour formater la date et l'heure en Java en utilisant la classe java.text.SimpleDateFormat.

Nom de fichier: 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); } } 
Testez-le maintenant

Sortir:

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 

Exemple Java SimpleDateFormat : chaîne à ce jour

Voyons l'exemple simple pour convertir une chaîne en date en utilisant la classe java.text.SimpleDateFormat.

Nom de fichier: 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();} } } 
Testez-le maintenant

Sortir:

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

Méthodes

set2DigitYearStart()

Syntaxe:

 public void set2DigitYearStart(Date startDate) 

Paramètres:

startDate : la date a été définie dans la plage - startDate à startDate + 100 ans

Type de retour :

Le type de retour de la méthode est nul

Mise en œuvre:

Voyons comment implémenter la méthode dans le code.

Nom de fichier: 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(); } } } 

Sortir:

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

get2DigitYearStart()

Syntaxe:

 public Date get2DigitYearStart() 

Paramètres:

Aucun paramètre n'est requis pour cette méthode

Type de retour :

Le type de retour de la méthode est Date et renvoie le début de la période de 100 ans définie lors de l'analyse.

Mise en œuvre:

Voyons comment implémenter la méthode dans le code.

Nom de fichier: 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(); } } } 

Sortir:

 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 

versMotif()

Syntaxe:

 public String toPattern() 

Paramètres:

Aucun paramètre n'est requis pour cette méthode

Type de retour :

Le type de retour de la méthode est String et renvoie le modèle de format de date.

Mise en œuvre:

Voyons comment implémenter la méthode dans le code.

Nom de fichier: VersPattern.java

 // 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()); } } 

Sortir:

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

analyser()

Syntaxe:

 public Date parse() 

Paramètres:

Aucun paramètre n'est requis pour cette méthode

Type de retour :

Le type de retour de la méthode est Date et renvoie la date analysée.

Mise en œuvre:

Voyons comment implémenter la méthode dans le code.

Nom de fichier: Analyser.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(); } } } 

Sortir:

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

appliquerMotif()

Syntaxe:

 public void applyPattern() 

Paramètres:

Aucun paramètre n'est requis pour cette méthode

Type de retour :

Le type de retour de la méthode est void. La méthode ne renvoie donc rien.

Mise en œuvre:

Voyons comment implémenter la méthode dans le code.

Nom de fichier: AppliquerPattern.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()); } } 

Sortir:

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

format()

Syntaxe:

 public final String format(Date args) 

Paramètres:

La méthode prend Date comme argument

Type de retour :

Le type de retour de la méthode est String et la méthode renvoie la chaîne formatée de la date.

Mise en œuvre:

Voyons comment implémenter la méthode dans le code.

Nom de fichier: 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); } } 

Sortir:

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

versMotifLocalisé()

Syntaxe:

 public String toLocalizedPattern() 

Paramètres:

La méthode ne prend aucun argument

Type de retour :

Le type de retour de la méthode est String et la méthode renvoie la chaîne de modèle de date du formateur de date.

Mise en œuvre:

Voyons comment implémenter la méthode dans le code.

Nom de fichier: ToLocalizedPattern.java

supprimer le premier caractère dans Excel
 // 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()); } } 

Sortir:

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