Question

I wanted to convert a date from one TimeZone to other. already had trouble with java.util.Date, So tried with JODA as suggested by people-

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.joda.time.DateTime;
    import org.joda.time.DateTimeZone;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;


    public class JodaTimeTest {
        public static void main(String[] args) throws ParseException {
            DateFormat formatter = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
            System.out.println(getDateStringToShow(formatter.parse("26-Nov-10 03:31:20 PM +0530"), "Asia/Calcutta", "Europe/Dublin", false));
            System.out.println(getDateStringToShow(formatter.parse("02-Oct-10 10:00:00 AM +0530"), "Asia/Calcutta", "Europe/Dublin", false));
            System.out.println(getDateStringToShow(formatter.parse("26-Nov-10 11:51:20 PM +0530"), "Asia/Kolkata", "Europe/Dublin", false));
            System.out.println(getDateStringToShow(formatter.parse("02-Oct-10 01:01:00 AM +0530"), "Asia/Kolkata", "Europe/Dublin", false));
        }

        public static String getDateStringToShow(Date date, String sourceTimeZoneId, String targetTimeZoneId, boolean includeTime) {
            DateTime dateRes = new DateTime(date);

            DateTime nowIndia = dateRes.toDateTime(DateTimeZone.forID(sourceTimeZoneId));
            DateTime nowDub = nowIndia.toDateTime(DateTimeZone.forID(targetTimeZoneId));

            DateTimeFormatter fmt = DateTimeFormat.forPattern("dd-MMM-yy z");
            System.out.println("nowIndia : " + fmt.print(nowIndia));
            System.out.println("nowDub : " + fmt.print(nowDub));
            return "---next---";
        }
    }

When Run-

    nowIndia : 26-Nov-10 IST
    nowDub : 26-Nov-10 GMT
    ---next---

    nowIndia : 02-Oct-10 IST
    nowDub : 02-Oct-10 IST
    ---next---

    nowIndia : 26-Nov-10 IST
    nowDub : 26-Nov-10 GMT
    ---next---

    nowIndia : 02-Oct-10 IST
    nowDub : 01-Oct-10 IST
    ---next---

Can anyone suggest me why its showing IST for date- 02-Oct-10 10:00:00 AM +0530

Was it helpful?

Solution

Like many time zone abbreviations, "IST" is not unique. It can stand for any of the following:

  • Indian Standard Time (UTC+05:30)
  • Israel Standard Time (UTC+02:00)
  • Irish Standard Time (UTC+01:00)

Europe/Dublin is the time zone id for Ireland. Like the United Kingdom, it uses the name "Greenwich Mean Time" during the winter. But in the summer, when the UK switches to "British Summer Time", Ireland switches to "Irish Standard Time".

Despite the word "standard" in the name, it is the daylight name for this time zone. If you see "Irish Summer Time" - that is in error.

In 2010, Europe/Dublin followed Irish Standard Time from March 28 through October 31, which explains why you see IST for 01-Oct-10 but GMT for 26-Nov-10.

India, on the other hand, follows Indian Standard Time all year round, so IST is always applicable.

For additional details, see:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top