The System.out.println(cal_Two.getTime())
invocation returns a Date
from getTime()
. It is the Date
which is getting converted to a string for println
, and that conversion will use the default IST
timezone in your case.
You’ll need to explicitly use DateFormat.setTimeZone()
to print the Date
in the desired timezone.
EDIT: Courtesy of @Laurynas, consider this:
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(timeZone);
System.out.println("Time zone: " + timeZone.getID());
System.out.println("default time zone: " + TimeZone.getDefault().getID());
System.out.println();
System.out.println("UTC: " + simpleDateFormat.format(calendar.getTime()));
System.out.println("Default: " + calendar.getTime());