2

I'm trying to read current system date and time format in java, but after analysing i couldn't find any method regarding this, so can anyone help me out.This is what i want read from java.

kadharbasha
  • 59
  • 2
  • 4
  • @Stultuske the question is about the date and time *format*. – JB Nizet Apr 02 '19 at 06:35
  • @JBNizet It's about the "current system date" and "time format" So, I assume they want the current system date (step 1) in a specific format (step 2). You can't have the result if you don't have the beginning. So, as long as the OP doesn't have LocalDateTime, DateTime, ... .now() or similar, throwing in a Formatter is a bit redundant – Stultuske Apr 02 '19 at 06:40
  • i just want to read date/time format, not current date/time in java. – kadharbasha Apr 02 '19 at 06:41
  • I posted a new and modern answer to the linked original question for you [here](https://stackoverflow.com/a/55469280/5772882). – Ole V.V. Apr 02 '19 at 07:39

3 Answers3

1

To get a DateTimeFormatter for the current system locale, you can use DateTimeFormatter.ofLocalizedDateTime()

The FormatStyle parameter controls whether you want long or short representation (Tuesday, April 12, 1952 AD 3:30:42pm PST versus 12.13.52 3:30pm).


A few examples and what I get on my local system:

//Tuesday, 2 April 2019 at 5:49:39 pm Australian Eastern Daylight Time
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).format(ZonedDateTime.now())

//2 April 2019 at 5:50:20 pm AEDT
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).format(ZonedDateTime.now())

//2/4/19, 5:50 pm
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).format(ZonedDateTime.now())

prunge
  • 22,460
  • 3
  • 73
  • 80
0

You can ask system current time only in milliseconds. And then you can transform them into DateTime:

long timestamp = System.currentTimeMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");    
Date date = new Date(timestamp);
System.out.println(dateFormat.format(date));
Scrobot
  • 1,911
  • 3
  • 19
  • 36
  • 1
    the question is about the format configured in the operating system. – entreprenr Apr 02 '19 at 06:44
  • 2
    Please don’t teach the young ones to use the notoriously troublesome and long outdated `SimpleDateFormat` class. Today we have so much better in [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Apr 02 '19 at 06:48
0

You can't do it with just the Java API. The system date-time format is operating system specific, and Java has no API to read that (you can read the date-time, and the locale, but not the format). You would have to find some library that does it or write it yourself - but beware that this is going to be vastly different for every OS. E.g. for Windows you might have to read the Windows registry.

To get the locale: Locale.getDefault()

From the locale, you can probably assume the format in most cases (but not if the user chose an untypical format).

entreprenr
  • 346
  • 2
  • 13