Display Date and Time Information in Uppercase in Java



In this article, we will learn to display the date and time in uppercase using Java. Working with date and time in Java is a common requirement in various applications, logging, scheduling, or data processing.

Different Approaches

The following are the two different approaches to display the date and time in uppercase using Java ?

Using Formatter and Calendar

The Formatter class in Java allows us to format date and time using the %Tc specifier. We can then convert the output to uppercase using toUpperCase() method.

Firstly, create a formatter and a Calendar object ?

Formatter f = new Formatter();
Calendar c = Calendar.getInstance();

To display complete date and time information use the ?c' conversion character. However, to display it in uppercase, use ?Tc' ?

f = new Formatter();
System.out.println(f.format("\nDate and Time (uppercase): %Tc\n", cal));

Example

Below is an example to display the date and time in uppercase using DateTimeFormatter ?

import java.util.Calendar;
import java.util.Formatter;
public class Demo {
   public static void main(String args[]) {
      Formatter f = new Formatter();
      Calendar cal = Calendar.getInstance();
      System.out.println("Current date and time: "+cal.getTime());
      f = new Formatter();
      System.out.println(f.format("\nDate and Time (uppercase): %Tc\n", cal));
   }
}

Output

Current date and time: Mon Nov 26 07:44:39 UTC 2018
Date and Time (lowercase): MON NOV 26 07:44:39 UTC 2018

Time Complexity: O(1), fetching the current time and formatting operations take constant time.
Space Complexity: O(1), uses a fixed amount of memory.

Using DateTimeFormatter

The java.time package introduced in Java 8 provides a more modern and efficient way to work with date and time. We can use DateTimeFormatter with toUpperCase() to achieve the same result.

Following are the steps to display the date and time in uppercase using DateTimeFormatter ?

  • Use LocalDateTime to get the current date and time.
  • Format it using DateTimeFormatter.
  • Convert the formatted string to uppercase using toUpperCase().
  • Display the result.

Define a formatter for date and time ?

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy hh:mm:ss a");

Format and convert to uppercase ?

String formattedDateTime = now.format(formatter).toUpperCase();

Example

Below is an example to display the date and time in uppercase using DateTimeFormatter ?

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeUppercase {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy hh:mm:ss a");

        String formattedDateTime = now.format(formatter).toUpperCase();
        System.out.println(formattedDateTime);
    }
}

Output

MONDAY, MARCH 03, 2025 12:08:05 PM

Time Complexity: O(1), constant time operation.
Space Complexity: O(1), uses a fixed amount of space.

Conclusion

Displaying date and time in uppercase can be useful in various scenarios, such as log files and reports. We explored two methods, using Formatter and Calendar this is a legacy approach, suitable for Java 7 and earlier. Using DateTimeFormatter and LocalDateTime this uses a modern approach, recommended for Java 8+.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-03-03T18:48:48+05:30

470 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements