Not getting the correct timezone in java

The code that I am using to get the current timezone in java side is as follows:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));

System.out.println("the timezone is--->"+(TimeZone.getTimeZone("America/Los_Angeles")).getDisplayName());

It is displaying as pacific Standard Time but the current time zone in america/los angeles is pacific daylight time

Can anyone help me with this?

Jon Skeet
people
quotationmark

You're calling the parameterless getDisplayName() method whose documentation includes:

This method is equivalent to:

 getDisplayName(false, LONG, Locale.getDefault(Locale.Category.DISPLAY))

The false here is the argument which specifies "don't use daylight saving time". So the parameterless method will never display a DST variation.

It's not clear that you really should usually display a name based on the current standard/daylight part - but if you really want to, you can use:

String name = zone.getDisplayName(zone.inDaylightTime(new Date()), TimeZone.LONG,
                                  Locale.getDefault(Locale.Category.DISPLAY));

Obviously you should customize the second and third parameters according to your requirements.

people

See more on this question at Stackoverflow