【问题标题】:JODA api convert date between timezonesJODA api在时区之间转换日期
【发布时间】:2014-05-01 07:35:04
【问题描述】:

我想将日期从一个时区转换为另一个。 java.util.Date 已经有问题了, 所以按照人们的建议尝试使用 JODA-

    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---";
        }
    }

运行时-

    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---

谁能告诉我为什么它显示日期为 IST-02-Oct-10 10:00:00 AM +0530

【问题讨论】:

    标签: java datetime timezone jodatime


    【解决方案1】:

    像许多时区缩写一样,"IST" 不是唯一的。它可以代表以下任何一种:

    • 印度标准时间 (UTC+05:30)
    • 以色列标准时间 (UTC+02:00)
    • 爱尔兰标准时间 (UTC+01:00)

    Europe/Dublin 是爱尔兰的时区 ID。与英国一样,它在冬季使用“格林威治标准时间”的名称。但是在夏天,当英国切换到“英国夏令时间”时,爱尔兰切换到“爱尔兰标准时间”。

    尽管名称中有“标准”一词,但它是该时区的日光名称。如果您看到“爱尔兰夏令时间” - 那是错误的。

    在 2010 年,Europe/Dublin 遵循爱尔兰标准时间 from March 28 through October 31,这就解释了为什么您看到 IST 是 2010 年 10 月 1 日,而 GMT 是 2010 年 11 月 26 日。

    另一方面,印度全年都遵循印度标准时间,因此 IST 始终适用。

    更多详情,请参阅:

    【讨论】:

      猜你喜欢
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多