ในบทความนี้เราจะมาทำความเข้าใจวิธีแสดงเวลาในรูปแบบประเทศต่างๆ Java ไม่มีคลาส Date ในตัว แต่เราสามารถนำเข้าแพ็คเกจ java.time เพื่อทำงานกับ dateand time API แพ็คเกจนี้รวมคลาสวันและเวลามากมาย
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
สมมติว่าข้อมูลที่เราป้อนคือ −
Run the program
ผลลัพธ์ที่ต้องการจะเป็น −
The England Format is: Friday, 18 March 2022 The Italian Format is: venerdì, 18 marzo 2022
อัลกอริทึม
Step 1 - START Step 2 - Declare an object of LocalDateTime namely date. Step 3 - Define the values. Step 4 - Define different date time formats using DateTimeFormatter objects. Step 5 - Display the different date time formats of different countries. Step 6 - Stop
ตัวอย่างที่ 1
ที่นี่ เราเชื่อมโยงการดำเนินการทั้งหมดเข้าด้วยกันภายใต้ฟังก์ชัน 'หลัก'
import java.text.DateFormat;
import java.util.*;
public class Demo {
public static void main(String[] args) throws Exception{
System.out.println("The required packages have been imported");
Date date_time = new Date();
Locale England_time = new Locale("en", "ch");
DateFormat de = DateFormat.getDateInstance(DateFormat.FULL, England_time);
System.out.println("\nThe England Format is: " + de.format(date_time));
Locale Italy_time = new Locale("it", "ch");
DateFormat di = DateFormat.getDateInstance(DateFormat.FULL, Italy_time);
System.out.println("The Italian Format is: " + di.format(date_time));
}
} ผลลัพธ์
The required packages have been imported The England Format is: Tuesday, March 29, 2022 The Italian Format is: marted?, 29. marzo 2022
ตัวอย่างที่ 2
ในที่นี้ เราสรุปการดำเนินการเป็นฟังก์ชันที่แสดงการเขียนโปรแกรมเชิงวัตถุ
import java.text.DateFormat;
import java.util.*;
public class Demo {
static void Time_formats(Date date_time ){
Locale England_time = new Locale("en", "ch");
DateFormat de = DateFormat.getDateInstance(DateFormat.FULL, England_time);
System.out.println("\nThe England Format is: " + de.format(date_time));
Locale Italy_time = new Locale("it", "ch");
DateFormat di = DateFormat.getDateInstance(DateFormat.FULL, Italy_time);
System.out.println("The Italian Format is: " + di.format(date_time));
}
public static void main(String[] args) throws Exception{
System.out.println("The required packages have been imported");
Date date_time = new Date();
System.out.println("A date object has been defined");
Time_formats(date_time);
}
} ผลลัพธ์
The required packages have been imported The England Format is: Tuesday, March 29, 2022 The Italian Format is: marted?, 29. marzo 2022