Calender class and changed TimeZone conflicts

I would like to get the date format as yyyy-mm-ddT00:00:00 and different TimeZone as an output. But here i am getting the default Time Zone in the output to calendar date.

Code part as:-
Calendar cal =  Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.set(Calendar.YEAR, 2014);
    cal.set(Calendar.MONTH, Calendar.NOVEMBER);
    cal.set(Calendar.DATE, 18);
    cal.set(Calendar.HOUR, 20);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
System.out.println("DateTime : " +  cal.getTime()+ " \n Zone :: "+ calFrom.getTimeZone().getDisplayName());

The output as:

DateTime : Wed Nov 19 05:30:00 IST 2014 
Zone :: Greenwich Mean Time

Here, i set the Time zone as GMT but in date it shows IST and how it can be converted to 2014-11-19T00:00:00 corrosponding to the changed Time Zone.

Jon Skeet
people
quotationmark

I would like to get the date format as yyyy-mm-ddT00:00:00 and different TimeZone as an output.

Then you need to be using a DateFormat of some description, e.g. SimpleDateFormat. In particular, a DateFormat knows about:

  • The format
  • The locale to use for things like month names
  • The time zone
  • The calendar system

Date - which is what Calendar.getTime() returns - doesn't know about any of that. It just represents a point in time, which can be represented in many different ways.

You probably want something like:

TimeZone zone = TimeZone.getTimeZone("Etc/UTC");
Calendar cal =  Calendar.getInstance(zone);
cal.clear(Calendar.MILLISECOND);
cal.set(2014, Calendar.NOVEMBER, 18, 20, 0, 0); // Why 20 if you want an hour of 0?

DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
format.setTimeZone(zone);

String text = format.format(cal.getTime());

people

See more on this question at Stackoverflow