
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Time in Different Countries' Format in Java
In this article, we will learn how to display the current date in different country formats using Java. 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 java.time package, along with DateFormat and Locale classes, allows us to format the date according to various regions' standards.
Problem Statement
Write a program in Java to display time in different country's formats. Below is a demonstration of the same ?
Input
Run the program
Output
The England Format is: Friday, 18 March 2022 The Italian Format is: venerdì, 18 marzo 2022
Different Approaches
Below are the different approaches to display time in different country's format ?
Using main() method
The following are the steps to display time in different country's format using main() method ?
- We will start by importing the DateFormat from java.text and Locale from java.util.
- We will create date object by using Date() to get the current date and will create a Locale for England.
- Format date for England by using DateFormat.getDateInstance() with the FULL format for England.
- Print England format and create a Locale for Italy.
- Format the date for Italy and use DateFormat.getDateInstance() for Italy's format.
- Print Italian format and output the formatted date for Italy.
Example
Here, we bind all the operations together under the main method ?
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
Using encapsulation
Following are the steps to display time in different country's formats using encapsulation ?
- First we will import the DateFormat and Locale.
- We will create a static method Time_formats(Date date_time) for formatting dates.
- Inside the method, format the date for England using DateFormat and print.
- Format the date for Italy and print.
- Define a Date object in main().
- Pass the Date object to Time_formats() for formatting and printing both dates.
Example
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