Marcio Cunha

Java: get system time, language and screen resolution

Java applications often need to know the current time, language, and screen size to adjust how they look and behave. This guide breaks down native, ready-to-use code snippets to fetch all three details smoothly.

Marcio Cunha7 min
Also available in:EspañolPortuguês
Summary
  • Modern Java projects should always use the java.time package for safe and clean date handling.
  • Desktop applications benefit from caching screen resolution data to avoid repeated heavy hardware lookups.
  • Handling global audiences requires ZonedDateTime to correctly manage and convert different time zones.
  • Native Java classes provide straightforward ways to read system time, user language, and screen size without external libraries.
  • Combining time, locale, and display metrics takes only a few concise lines of code to build adaptive applications.

Introduction

Java apps often need the system time, locale, which is the user's language and regional setting, and screen size to adapt the user interface. Below is a simple, native approach.

1. System time (Java 8+)

import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;LocalDateTime now = LocalDateTime.now();DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");System.out.println(now.format(fmt));

2. Language and locale

import java.util.Locale;Locale locale = Locale.getDefault();System.out.println(locale.getLanguage());System.out.println(locale.getCountry());System.out.println(locale.getDisplayName());

3. Screen resolution

import java.awt.Toolkit;import java.awt.Dimension;Toolkit toolkit = Toolkit.getDefaultToolkit();Dimension size = toolkit.getScreenSize();System.out.println(size.width + "x" + size.height);

Quick full example

import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.util.Locale;import java.awt.Toolkit;import java.awt.Dimension;LocalDateTime now = LocalDateTime.now();Locale locale = Locale.getDefault();Dimension size = Toolkit.getDefaultToolkit().getScreenSize();System.out.println(now.format(DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss")));System.out.println(locale.getDisplayName());System.out.println(size.width + "x" + size.height);

Best practices

  • Use java.time in new projects.
  • Cache screen info in desktop apps.
  • Use ZonedDateTime for time zones.

Conclusion

With a few lines of code, you can read time, locale and screen resolution and build more adaptive apps.