0

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.

Tarun Chaudhary
  • 1,046
  • 2
  • 12
  • 20

2 Answers2

4

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());
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • there is a didifference of 6 Hours and 30 minutes in IST and GMT. if i put cal.set(Calendar.HOUR, 6); cal.set(Calendar.MINUTE, 30); then only i am getting 00:00:00. – Tarun Chaudhary Nov 18 '14 at 19:16
  • 1
    @TarunChaudhary: No, IST is UTC+05:30. (GMT and UTC are equivalent, for the purposes of this discussion - but it's generally better to talk about UTC to avoid confusing things with the UK time zone, which observes GMT for half the year.) – Jon Skeet Nov 18 '14 at 19:17
  • 1
    @TarunChaudhary: What do you mean by that? It's unclear what you're trying to achieve *and* what you're actually seeing... along with the problem of being 1 hour out in your understanding of IST. – Jon Skeet Nov 18 '14 at 19:18
1

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdtUtc = LocalDate.of(2014,Month.NOVEMBER, 18)
                                        .atTime(LocalTime.of(20, 0))
                                        .atZone(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc);
    }
}

Output:

2014-11-18T20:00Z[Etc/UTC]

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Explanation of the problem you are facing:

A java.util.Date object simply represents an instant on the timeline — a wrapper around the number of milliseconds since the UNIX epoch (January 1, 1970, 00:00:00 GMT). Since it does not hold any timezone information, its toString function applies the JVM's timezone to return a String in the format, EEE MMM dd HH:mm:ss zzz yyyy, derived from this milliseconds value. To get the String representation of the java.util.Date object in a different format and timezone, you need to use SimpleDateFormat with the desired format and the applicable timezone e.g.

Date date = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);

sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String strDateNewYork = sdf.format(date);

sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String strDateUtc = sdf.format(date);

A valuable comment from Ole V.V.:

If we cannot avoid getting an old-fashioned Calendar object, by all likelihood it’s really a GregorianCalendar, and we can convert it to ZonedDateTime simply by using ((GregorianCalendar) cal).toZonedDateTime(). Then we’re set.

Another one:

Or if using a backport: DateTimeUtils.toZonedDateTime(cal).


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    This is the good answer. If we cannot avoid getting an old-fashioned `Calendar` object, by all likelyhood it’s really a `GregorianCalendar`, and we can convert it to `ZonedDateTime` simply by using `((GregorianCalendar) cal).toZonedDateTime()`. Then we’re set. – Ole V.V. Jul 03 '21 at 09:46