[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 760
  • Last Modified:

Java Calendar Timezone issue

Appreciate your help in pin pointing the issue in following code. I am creating a Calendar object of a specific time zone, but it is still displaying the local time zone.
e.g.  I have currently US/Eastern DST

Date currentDate = new Date();
TimeZone timeZone = TimeZone.getTimeZone("Canada/Newfoundland")
Calendar calTime = Calendar.getInstance(timeZone);
calTime.setTime(baseDate);

I am expecting that calTime will print the current time in NewFoundLand timezone, but when I print the value of calTime.getTime() it is printing time in US Eastern.  I tried to check at line 3 and line 4, at both lines it is printing US Eastern time.

What changes needed to make so that it print NewfoundLand Time?   Thanks,

0
fix2009
Asked:
fix2009
  • 3
2 Solutions
 
objectsCommented:
how do you display it, the wrapped date does *not* have any timezone associated with it
So if you are using DateFormat to display the date then you need to set the timezone for the DateFormat

0
 
fix2009Author Commented:
Ok. Let me simplify the question. How to display the NewfoundLand time being in a different timezone using Calendar or Date objects.
0
 
objectsCommented:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
df.setTimeZone(timeZone);
String formatted = df.format(calTime.getTime());

0
 
objectsCommented:
or just:

String formatted = df.format(currentDate);

0
 
CEHJCommented:
>>How to display the NewfoundLand time being in a different timezone using Calendar or Date objects.

DateFormat df = DateFormat.getInstance();
df.setTimeZone(timeZone);
System.out.println(df.format(calTime.getTime()));

Open in new window

0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now