How to create Calendar with specific TimeZone?

I'm creating a Calendar object:

Calendar calendar = new GregorianCalendar(2014, 0, 1);

and calendar.getTime() returns Wed Jan 01 00:00:00 BRT 2014

that is2014-01-01T00:00:00.000-0300

How to create Calendar with specific date in UTC TimeZone?

When I try to do calendar.setTimeZone(TimeZone.getTimeZone("UTC"));

calendar.getTime() returns the same.

Jon Skeet
people
quotationmark

Just reverse the order of "specify date, specify time zone" to "specify time zone, specify date":

Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.set(2014, 0, 1, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);

I'd also recommend avoiding the Calendar/Date API entirely - use java.time for Java 8, and Joda Time for older versions of Java.

people

See more on this question at Stackoverflow