logo

Java SimpleDateFormat

La classe java.text.SimpleDateFormat fornisce metodi per formattare e analizzare data e ora in Java. SimpleDateFormat è una classe concreta per la formattazione e l'analisi della data che eredita la classe java.text.DateFormat.

Notare che formattare significa convertire la data in una stringa E analizzare significa convertire la stringa fino ad oggi .

Costruttori della classe SimpleDateFormat

SimpleDateFormat(String pattern_args): Crea un'istanza della classe SimpleDateFormat utilizzando il modello fornito: pattern_args, simboli del formato data predefinito per le impostazioni internazionali FORMAT predefinite.

SimpleDateFormat(String pattern_args, Locale locale_args): Crea un'istanza della classe SimpleDateFormat utilizzando il modello fornito: pattern_args. Per la lingua FORMAT fornita, i simboli del formato data predefinito sono - locale_args.

SimpleDateFormat(String pattern_args, DateFormatSymbols formatSymbols): Crea un'istanza della classe SimpleDateFormat e utilizza il modello fornito - pattern_args e il formato della dataSymbols.

Esempio Java SimpleDateFormat: data in stringa

Vediamo il semplice esempio a formato data in Java utilizzando la classe java.text.SimpleDateFormat.

Nome del file: SimpleDateFormatExample.java

sottostringa in bash
 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); } } 
Provalo adesso

Produzione:

13/04/2015 

Nota: M (M maiuscola) rappresenta il mese e m (m minuscola) rappresenta i minuti in Java.

Vediamo l'esempio completo formato data e ora in Java utilizzando la classe java.text.SimpleDateFormat.

Nome del file: 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); } } 
Provalo adesso

Produzione:

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 

Esempio Java SimpleDateFormat: stringa fino alla data

Vediamo il semplice esempio a convertire la stringa in data utilizzando la classe java.text.SimpleDateFormat.

Nome del file: 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();} } } 
Provalo adesso

Produzione:

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

Metodi

set2DigitYearStart()

Sintassi:

 public void set2DigitYearStart(Date startDate) 

parametri:

startDate: la data è stata impostata nell'intervallo: startDate fino a startDate + 100 anni

Tipo di reso:

Il tipo restituito del metodo è void

Implementazione:

Vediamo come implementare il metodo nel codice.

Nome del file: 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(); } } } 

Produzione:

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

get2DigitYearStart()

Sintassi:

 public Date get2DigitYearStart() 

parametri:

Nessun parametro è richiesto per questo metodo

Tipo di reso:

Il tipo restituito del metodo è Date e restituisce l'inizio del periodo di 100 anni impostato durante l'analisi.

Implementazione:

Vediamo come implementare il metodo nel codice.

Nome del file: 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(); } } } 

Produzione:

 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 

almodello()

Sintassi:

 public String toPattern() 

parametri:

Nessun parametro è richiesto per questo metodo

Tipo di reso:

Il tipo restituito del metodo è String e restituisce il modello del formato della data.

Implementazione:

copertura delle dichiarazioni

Vediamo come implementare il metodo nel codice.

Nome del file: ToPattern.java

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

Produzione:

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

analizzare()

Sintassi:

 public Date parse() 

parametri:

Nessun parametro è richiesto per questo metodo

Tipo di reso:

Il tipo restituito del metodo è Date e restituisce la data analizzata.

Implementazione:

Vediamo come implementare il metodo nel codice.

Nome del file: Analizza.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(); } } } 

Produzione:

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

applicamodello()

Sintassi:

 public void applyPattern() 

parametri:

Nessun parametro è richiesto per questo metodo

Tipo di reso:

Il tipo restituito del metodo è void. Pertanto, il metodo non restituisce nulla.

Implementazione:

Vediamo come implementare il metodo nel codice.

Nome del file: ApplicaModello.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()); } } 

Produzione:

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

formato()

Sintassi:

 public final String format(Date args) 

parametri:

Il metodo accetta Date come argomento

commento xml

Tipo di reso:

Il tipo restituito del metodo è String e il metodo restituisce la stringa formattata della data.

Implementazione:

Vediamo come implementare il metodo nel codice.

Nome del file: Formato.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); } } 

Produzione:

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

aLocalizedPattern()

Sintassi:

 public String toLocalizedPattern() 

parametri:

Il metodo non accetta alcun argomento

Tipo di reso:

Il tipo restituito del metodo è String e il metodo restituisce la stringa del modello Date del formattatore della data.

Implementazione:

Vediamo come implementare il metodo nel codice.

Nome del file: ToLocalizedPattern.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()); } } 

Produzione:

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