Java: get system time, language and screen resolution
Straight to the point with ready-to-use code.

Introduction

Java apps often need the system time, locale and screen size to adapt the UI. 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.