Java 17 Recipes


-17. Formatting Dates for Display


Download 3.2 Mb.
Pdf ko'rish
bet87/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   83   84   85   86   87   88   89   90   ...   245
Bog'liq
Java 17 Recipes

4-17. Formatting Dates for Display
 Problem
Dates need to be displayed by your application using a specific format. You want to 
define that format once and apply it to all dates that need to be displayed.
 Solution 1
Utilize the DateTimeFormatter class, part of the Date-Time API, to format dates and 
times according to the pattern you want to use. The DateTimeFormatter class includes 
an ofPattern() method, which accepts a string pattern argument to designate the 
desired pattern. Each of the temporal date-time classes includes a format() method, 
which accepts a DateTimeFormatter and returns the string-based format of the target 
date-time object. In the following lines of code, the DateTimeFormatter is demonstrated.
Chapter 4 Numbers aNd dates


144
public static void formatting() {
try {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern 
("MMMM dd yyyy");
LocalDateTime now = LocalDateTime.now();
String output = now.format(dateFormatter);
System.out.println(output);
DateTimeFormatter dateFormatter2 = DateTimeFormatter.ofPattern 
("MM/dd/YY HH:mm:ss");
String output2 = now.format(dateFormatter2);
System.out.println(output2);
DateTimeFormatter dateFormatter3 = DateTimeFormatter.ofPattern 
("hh 'o''clock' a, zzzz");
ZonedDateTime zdt = ZonedDateTime.now();
String output3 = zdt.format(dateFormatter3);
System.out.println(output3);
} catch (DateTimeException ex) {
System.out.println("Cannot be formatted: " + ex);
}
}
Here is the result.
ottobre 13 2021
10/13/21 22:44:00
10 o'clock PM, Ora legale dell’Europa centrale
 Solution 2
Use the java.util.Calendar class to obtain the date that you require and then format 
that date using the java.text.SimpleDateFormat class. The following example 
demonstrates the use of the SimpleDateFormat class.
public static void formatExamplesCalendar() {
// Create new calendar
Chapter 4 Numbers aNd dates


145
Calendar cal = Calendar.getInstance();
// Create instance of SimpleDateFormat class using pattern
SimpleDateFormat dateFormatter1 = new SimpleDateFormat 
("MMMMM dd yyyy");
String result = null;
result = dateFormatter1.format(cal.getTime());
System.out.println(result);
dateFormatter1.applyPattern("MM/dd/YY hh:mm:ss");
result = dateFormatter1.format(cal.getTime());
System.out.println(result);
dateFormatter1.applyPattern("hh 'o''clock' a, zzzz");
result = dateFormatter1.format(cal.getTime());
System.out.println(result);
}
Running this example would yield the following result.
ottobre 13 2021
10/13/21 10:44:01
10 o'clock PM, Ora legale dell’Europa centrale
The main is:
public static void main(String[] args) {
formatting();
formatExamplesCalendar();
}
As you can see from the results, the DateTimeFormatter and SimpleDateFormat 
classes make it easy to convert a date into just about any format.
Chapter 4 Numbers aNd dates


146

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   83   84   85   86   87   88   89   90   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling