logo

Java SimpleDateFormat

A java.text.SimpleDateFormat osztály módszereket biztosít a dátum és idő formázására és elemzésére a java-ban. A SimpleDateFormat egy konkrét osztály a dátum formázására és elemzésére, amely örökli a java.text.DateFormat osztályt.

Vedd észre A formázás a dátum karakterláncsá alakítását jelenti és Az elemzés a karakterlánc dátummá alakítását jelenti .

A SimpleDateFormat osztály konstruktőrei

SimpleDateFormat(String pattern_args): Példányosítja a SimpleDateFormat osztályt a megadott mintával - minta_args, az alapértelmezett dátumformátum szimbólumok az alapértelmezett FORMAT területi beállításhoz.

SimpleDateFormat(String minta_args, Locale locale_args): Példányosítja a SimpleDateFormat osztályt a megadott mintával - minta_args. A megadott FORMAT Locale esetén az alapértelmezett dátumformátum szimbólumok a - locale_args.

SimpleDateFormat(String pattern_args, DateFormatSymbols formatSymbols): Példányosítja a SimpleDateFormat osztályt és a megadott mintát - minta_args és a dátum formatSymbols.

Java SimpleDateFormat Példa: Date to String

Lássuk az egyszerű példát dátum formátuma java-ban a java.text.SimpleDateFormat osztály használatával.

Fájl név: SimpleDateFormatExample.java

sqrt java matek
 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); } } 
Tesztelje most

Kimenet:

13/04/2015 

Megjegyzés: M (nagy M) a hónapot, m (kis m) pedig percet jelent Java nyelven.

Lássuk a teljes példát formátumban a dátumot és az időt Java-ban a java.text.SimpleDateFormat osztály használatával.

Fájl név: 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); } } 
Tesztelje most

Kimenet:

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 Példa: String to Date

Lássuk az egyszerű példát konvertálja a karakterláncot dátummá a java.text.SimpleDateFormat osztály használatával.

Fájl név: 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();} } } 
Tesztelje most

Kimenet:

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

Mód

set2DigitYearStart()

Szintaxis:

 public void set2DigitYearStart(Date startDate) 

Paraméterek:

startDate: A dátum a következő tartományban van beállítva: startDate to startDate + 100 év

Visszaküldés típusa:

A metódus visszatérési típusa érvénytelen

Végrehajtás:

Nézzük meg, hogyan valósítható meg a módszer a kódban.

Fájl név: 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(); } } } 

Kimenet:

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

get2DigitYearStart()

Szintaxis:

 public Date get2DigitYearStart() 

Paraméterek:

Ehhez a módszerhez nincs szükség paraméterre

Visszaküldés típusa:

A metódus visszatérési típusa a dátum, és az elemzés során beállított 100 éves periódus kezdetét adja vissza.

Végrehajtás:

Nézzük meg, hogyan valósítható meg a módszer a kódban.

Fájl név: 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(); } } } 

Kimenet:

 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()

Szintaxis:

 public String toPattern() 

Paraméterek:

Ehhez a módszerhez nincs szükség paraméterre

Visszaküldés típusa:

A metódus visszatérési típusa String, és a dátumformátum mintáját adja vissza.

Végrehajtás:

hogyan lehet visszaadni a tömböt java-ban

Nézzük meg, hogyan valósítható meg a módszer a kódban.

Fájl név: ToPattern.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()); } } 

Kimenet:

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

parse()

Szintaxis:

 public Date parse() 

Paraméterek:

Ehhez a módszerhez nincs szükség paraméterre

youtube videók letöltése vlc

Visszaküldés típusa:

A metódus visszatérési típusa Date, és az elemzés dátumát adja vissza.

Végrehajtás:

Nézzük meg, hogyan valósítható meg a módszer a kódban.

Fájl név: 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(); } } } 

Kimenet:

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

alkalmazPattern()

Szintaxis:

 public void applyPattern() 

Paraméterek:

Ehhez a módszerhez nincs szükség paraméterre

Visszaküldés típusa:

A metódus visszatérési típusa érvénytelen. Ezért a módszer nem ad vissza semmit.

Végrehajtás:

Nézzük meg, hogyan valósítható meg a módszer a kódban.

Fájl név: 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()); } } 

Kimenet:

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

formátum()

Szintaxis:

 public final String format(Date args) 

Paraméterek:

A metódus a dátumot veszi argumentumként

Visszaküldés típusa:

A metódus visszatérési típusa String, és a metódus a dátum formázott karakterláncát adja vissza.

Végrehajtás:

Nézzük meg, hogyan valósítható meg a módszer a kódban.

Fájl név: 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); } } 

Kimenet:

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

toLocalizedPattern()

Szintaxis:

 public String toLocalizedPattern() 

Paraméterek:

b+ fák

A módszer nem fogad el érveket

Visszaküldés típusa:

A metódus visszatérési típusa String, a metódus pedig a dátumformázó Dátum minta karakterláncát adja vissza.

Végrehajtás:

Nézzük meg, hogyan valósítható meg a módszer a kódban.

Fájl név: 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()); } } 

Kimenet:

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