0

I want to get date in jave for nl_NL locale

Calendar calendar = Calendar.getInstance(); 
DateFormat df = new SimpleDateFormat(Pattern, new Locale("nl_NL"));
df.setTimeZone(TimeZone.getTimeZone("PST"));
String expectedDate = df.format(calendar.getTime()).toLowerCase();
System.out.println("Date in PST Timezone : " + expectedDate);
return expectedDate;

I am not getting it in correct nl_NL format!

can someone please help me in this ?

  • does this answer your question? https://stackoverflow.com/questions/8770726/java-date-format-conversion-getting-wrong-month – Joni Jul 17 '20 at 15:23
  • `MonthDay.now(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("MMM dd", Locale.forLanguageTag("nl-NL")))` just gave me `jul. 20` Is this what you were after? – Ole V.V. Jul 20 '20 at 18:14

2 Answers2

3

Never use 2-4 character pseudo-zones such as PST, CST, or IST. These values are not standardized, and are not even unique!

Real time zone names are in the format of Continent/ Region such as Africa/Tunis or Asia/Tokyo. For the time zone of most of the west coast of US, if that is what you meant by PST, use America/Los_Angeles.

ZoneId z = ZoneId.of( "America/Los_Angeles" ) ; 

Never use the terrible legacy date-time classes such as Calendar. Use only java.time classes.

ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Generate text in standard ISO 8601 format wisely extended by appending the name of the time zone in square brackets.

String output = zdt.toString() ;

Localize a textual representation for Dutch language and Netherlands culture.

Locale locale = new Locale( "nl" , "NL" ) ;
DateTimeFormatter f = 
    DateTimeFormatter
    .ofLocalizedDateTime( FormatStyle.FULL )
    .withLocale( locale ) 
;
String output = zdt.format( f ) ;

See this code run live at IdeOne.com.

2020-07-17T09:30:58.183568-07:00[America/Los_Angeles]

vrijdag 17 juli 2020 om 09:30:58 Pacific-zomertijd

Search to learn more. All this has been addressed many many times already on Stack Overflow.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

I am not getting it in correct nl_NL format

That is because new Locale("nl_NL") is wrong. It must be either new Locale("nl", "NL") with separate language and country parameters, or Locale.forLanguageTag("nl-NL") with a languageTag parameter using a dash in the value. It is never with an underscore.

It is also because date pattern "mmm dd" is wrong. It must be "MMM dd" with M's in uppercase to represent month, not in lowercase which represents minute.

And finally, using time zone PST is wrong, because it is not well-defined. It can mean "Pitcairn Standard Time" or "Pacific Standard Time", and even that second one is wrong, because the US Pacific coast is currently observing PDT (Pacific Daylight Time). The correct time zone is America/Los_Angeles.

Applying those fixes to the question code:

Calendar calendar = Calendar.getInstance(); 
DateFormat df = new SimpleDateFormat("MMM dd", new Locale("nl", "NL"));
df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
String expectedDate = df.format(calendar.getTime()).toLowerCase();
System.out.println("Date in PST Timezone : " + expectedDate);

Output

Date in PST Timezone : jul. 17

Of course, for a date-only value, time zone doesn't really apply, but there it is.


You should however use the newer Java 8 Time API.

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd", new Locale("nl", "NL"));
String date = MonthDay.now(ZoneId.of("America/Los_Angeles")).format(fmt);
System.out.println("Date : " + date);

Output

Date : jul. 17

You can also use LocaleDate.now(...).

Andreas
  • 154,647
  • 11
  • 152
  • 247