Java program to display time in different country’s format



In this article, we will learn how to display the current date in different country formats using Java. We can import java.time package to work with the date and time API.

The java.time package, along with the DateFormat and Locale classes, allows us to format the date according to various regions' standards.

Displaying Date in Different Country Formats

There are two or more ways to display the date in different country formats in Java:

Let's explore each of these methods in detail.

Using DateFormat Class

The DateFormat class is part of the java.text package. We can use it to format the date according to the locale of a specific country. It parses and formats dates in a locale-sensitive manner.

We use the getDateInstance() method of the DateFormat class to get an instance of DateFormat for a specific locale.

Syntax

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale locale);

Steps to Display Date in Different Country Formats

  • We will create a 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 the England format and create a Locale for Italy.
  • Format the date for Italy and use DateFormat.getDateInstance() for Italy's format.
  • Print the Italian format and output the formatted date for Italy.

Example

Following is an example that demonstrates how to display the current date in different country formats using the DateFormat class:

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class FormatDate {
   public static void main(String[] args) {
      Date date = new Date();

      Locale englandLocale = new Locale("en", "GB");

      DateFormat englandDateFormat = DateFormat.getDateInstance(DateFormat.FULL, englandLocale);
      String englandDate = englandDateFormat.format(date);
      System.out.println("Date in England format: " + englandDate);

      Locale italyLocale = new Locale("it", "IT");

      DateFormat italyDateFormat = DateFormat.getDateInstance(DateFormat.FULL, italyLocale);
      String italyDate = italyDateFormat.format(date);
      System.out.println("Date in Italy format: " + italyDate);
   }
}

Output

Following is the output of the above code:

Date in England format: Wednesday, 25 June 2023
Date in Italy format: domenica 25 giugno 2023

Using LocalDate Class and DateTimeFormatter

The LocalDate class is part of the java.time package was introduced in Java 8. It represents a date without a time-zone in the ISO-8601 calendar system. We can use it to get the current date and format it according to different locales.

We will use the now() method of the LocalDate class to get the current date and then format it using the format() method with a DateTimeFormatter.

Syntax

Following is the syntax to format a LocalDate object:

LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy", Locale locale);
String formattedDate = localDate.format(formatter);

Steps to Display Date in Different Country Formats

  • Get the current date using LocalDate.now().
  • Create a DateTimeFormatter with a specific pattern and locale.
  • Format the LocalDate using the formatter.

Example

Following is an example that demonstrates how to display the current date in different country formats using the LocalDate class:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class FormatLocalDate {
   public static void main(String[] args) {
      LocalDate localDate = LocalDate.now();

      DateTimeFormatter englandFormatter = DateTimeFormatter.ofPattern("dd MMMM yyyy", Locale.UK);
      String englandDate = localDate.format(englandFormatter);
      System.out.println("Date in England format: " + englandDate);

      DateTimeFormatter italyFormatter = DateTimeFormatter.ofPattern("dd MMMM yyyy", Locale.ITALY);
      String italyDate = localDate.format(italyFormatter);
      System.out.println("Date in Italy format: " + italyDate);
   }
}

Output

Following is the output of the above code:

Date in England format: 25 June 2023
Date in Italy format: 25 giugno 2023
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-10T14:47:26+05:30

639 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements