In this article, we will understand how to display time in different country’s format. Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes.
Below is a demonstration of the same −
Suppose our input is −
Run the program
The desired output would be −
The England Format is: Friday, 18 March 2022 The Italian Format is: venerdì, 18 marzo 2022
Algorithm
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
Example 1
Here, we bind all the operations together under the ‘main’ function.
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));
}
}Output
The required packages have been imported The England Format is: Tuesday, March 29, 2022 The Italian Format is: marted?, 29. marzo 2022
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
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);
}
}Output
The required packages have been imported The England Format is: Tuesday, March 29, 2022 The Italian Format is: marted?, 29. marzo 2022