
- 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 Languages Using Java
The DateFormat class allows you to format dates and times based on locale-specific settings in Java. You can use this class to display time and date in different languages by specifying a Locale object.
Displaying Time in Different Languages
To display time in different languages, you can use the DateFormat class in combination with the Locale class to format the time according to the desired language and region.
Locale class: The 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: The DateFormat class 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: The getDateInstance() method is a static method of the DateFormat class that returns an instance of DateFormat for formatting the date according to a specific locale.
Steps to Display Time in Different Languages
The following are the steps to display time in different languages
Step 1. Create a Date object: This creates a Date object holding the current date and time.
Date d1 = new Date();
Step 2. Print the current date and time: Display the current date and time using the default format.
System.out.println("today is " + d1.toString());
Step 3. Create a Locale for Italian: Initialize a Locale object for the Italian language.
Locale locItalian = new Locale("it");
Step 4. Create a DateFormat instance for full date format in Italian: Use DateFormat.getDateInstance() to create a date formatter in full format using the Italian locale.
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locItalian);
Step 5. Format and print the date in Italian: Format the current date in Italian and print it.
System.out.println("today is " + df.format(d1));
Java Program to Display Time in Different Languages
The following example uses the DateFormat class to display time in Italian
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"); DateFormat df = DateFormat.getDateInstance (DateFormat.FULL, locItalian); System.out.println("today is "+ df.format(d1)); } }
Output
today is Mon Dec 09 07:08:38 GMT 2024 today is luned? 9 dicembre 2024
Code Explanation
The program first creates a Date object (d1) representing the current date and time. Then, we create a Locale object for the Italian language (it). The DateFormat.getDateInstance(DateFormat.FULL, locItalian) method retrieves a DateFormat instance that formats the date according to the Italian language's full date format. Finally, the df.format(d1) method is used to format the current date (d1).