2

I am writing a JavaFx program that shows the Time and Date format of Windows. That means when I change the language of Windows, the program should show the new time and date format. For example in some locations, the time format is like this:

30.12.2018

and in other locations, it's displayed like this:

12.30.2018

I tried with LocalDateTime Class in Java, but that makes no sense, because that class doesn't have Timezone information.

private void init()
{
    button = new Button("Update");
    button.setFont(Font.font(null,FontWeight.BOLD,15));
    setBottom(button);
    setAlignment(button, Pos.CENTER);
    setPadding(new Insets(15));

    ldt = LocalDateTime.now();
    label = new Label();
    label.setFont(Font.font(null, FontWeight.BOLD,20));
    label.setTextFill(Color.RED);
    label.setAlignment(Pos.CENTER);

    text = ldt.toString();
    label.setText(text);

    gridPane = new GridPane();
    gridPane.add(label, 0, 0);
    gridPane.setAlignment(Pos.CENTER);
    setCenter(gridPane);        
}

German:

enter image description here

English:

enter image description here

How can I display this correctly?

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 3
    Then don't use a _Local_DateTime. Can you elaborate on your problem? – Marvin Feb 23 '19 at 17:57
  • I tried to explain my problem. when i change the timezone of windows (Language) the program should show me this change. but localdatetime has a predefined format and does not have to do with windows timezone. the Localdatetime shows me every time the same format such as 2007-12-03T10:15:30. but when i change the location of windows, the windows shows me a new date format like this : 03/12/2007 –  Feb 23 '19 at 18:18
  • 1
    Maybe this helps: https://stackoverflow.com/questions/6711925/how-can-i-set-date-and-time-formatting-in-java-that-respects-the-users-os-setti – Marvin Feb 23 '19 at 18:22

1 Answers1

8

Wrong class: LocalDateTime

Never use LocalDateTime to track an actual moment. Lacking any concept of time zone or offset-from-UTC, a LocalDateTime cannot represent a moment. A LocalDateTime can know “noon on the 23rd of January 2019” but it does not know if you mean noon in Tokyo Japan, noon in Tunis Tunisia, or noon in Montréal Québec — three very different moments, each separated by several hours.

The word “Local” in LocalDateTime means any locality, or every locality. But it does not mean any one particular locality.

Correct class: ZonedDateTime

To track moments use either Instant (always in UTC) or ZonedDateTime (or perhaps OffsetDateTime).

Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
ZonedDateTime zdt = ZonedDateTime.now( z ) ; // Capture the current moment as seen through the wall-clock time used by the people of a particular region (a time zone). 
Instant instant = zdt.toInstant() ;  // Adjust from a zone to UTC, if needed. An `Instant` is always in UTC by definition. 

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument. If critical, confirm the zone with your user.

Auto-localize

The DateTimeFormatter class can automatically localize while generating text representing the value of date-time object.

To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine:
    • The human language for translation of name of day, name of month, and such.
    • The cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Example:

Locale l = Locale.CANADA_FRENCH ;   // Or Locale.US, Locale.JAPAN, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL )
                                       .withLocale( l );
String output = zdt.format( f );

Be clear that locale and time zone have nothing to do with one another – orthogonal issues. A engineer from Québec attending a conference in India might want to see the schedule of events presented in her native French language with Canadian cultural norms, and with the date-times shown in the local Indian time zone.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 2
    This is a stellar answer and I wish there was a way for it to get more attention than getting buried in some random question. – corsiKa Feb 23 '19 at 21:58
  • @corsiKa There is - bounties. You can post a bounty to the question and pick one of the default reasons: "reward an existing answer". It will bump the post and also make the post appear higher up in the bounty filter of questions the closer it gets to the bounty's expiration date, and will also further reward the answerer by awarding an extra amount of 50 to 500 rep on top of whatever rep they earn. – TylerH Feb 24 '19 at 18:09
  • 2
    @Basil Note that people are talking about your answers which may be attracting meta-effect voting, and the general concensus seems to be going against you adding the same boilerplate text across multiple posts. https://meta.stackoverflow.com/questions/380536/same-aside-added-to-the-end-of-multiple-answers?cb=1 – DavidG Feb 25 '19 at 11:39
  • @TylerH Oh for sure. That will do it a little. Better, though, is for regulars to either close dupes or otherwise link to it. – corsiKa Feb 27 '19 at 05:36