
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to Display Time in Different Country's Format in Java
In this tutorial, we can display the date and time according to the formatting conventions of different countries or locales using the Locale class along with the DateFormat class in Java. The DateFormat.getDateInstance() method can be used to format the date according to the desired country's format.
Displaying Time in a Different Country's Format
Locale class: A Locale class provides information about a specific geographic, political, or cultural region. It helps in formatting data such as numbers, dates, and currencies according to a specific locale.
DateFormat class: A class that provides methods to format and parse dates in a language-sensitive manner, allowing you to represent dates and times according to different locales.
getDateInstance() method: A static method of the DateFormat class that returns an instance of DateFormat for formatting the date according to a specific locale.
Java Program to Display Time in a Different Country's Format
The following example uses the Locale class & DateFormat class to display dates in different Country's formats
import java.text.DateFormat; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Date d1 = new Date(); System.out.println("today is "+ d1.toString()); Locale locItalian = new Locale("it","ch"); DateFormat df = DateFormat.getDateInstance (DateFormat.FULL, locItalian); System.out.println("today is in Italian Language in Switzerland Format : "+ df.format(d1)); } }
Output
today is Mon Jun 22 02:37:06 IST 2009 today is in Italian Language in Switzerland Format : sabato, 22. febbraio 2014
Code Explanation
The program first creates a Date object (d1) which holds the current date and time. We then create a Locale object for Italy (it) and Switzerland (ch). The DateFormat.getDateInstance(DateFormat.FULL, locItalian) method is used to get a DateFormat instance that formats the date in the full format for the specified locale (Italian in Switzerland). Finally, the df.format(d1) method formats the current date (d1) and prints it in the Swiss-Italian format.