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(...)
.