【问题标题】:Convert IST to US timezones considering the daylight saving time in java考虑到 java 中的夏令时,将 IST 转换为美国时区
【发布时间】:2013-03-14 11:53:26
【问题描述】:

我在 IST 中有一个日期时间。考虑到夏令时,我想根据输入将其转换为美国时区, java中给定日期时间是否有夏令时。 这是我尝试过的

function convert(Date dt,int toTimeZoneId){
Calendar cal = Calendar.getInstance();
cal.setTime(dt); // Geting time in IST

//Converted to GMT and set in cal

switch(toTimeZoneId){
    case 1: tzTarget = TimeZone.getTimeZone("America/Adak"); 
        offset = -10;
        break;
    case 2: tzTarget = TimeZone.getTimeZone("America/Anchorage"); 
        offset = -9;
        break;
    case 3: tzTarget = TimeZone.getTimeZone("America/Los_Angeles"); 
        offset = -8;
        break;

    case 4: tzTarget = TimeZone.getTimeZone("America/Denver");
        offset = -7;
        break;
    case 5: tzTarget = TimeZone.getTimeZone("America/Chicago");
        offset = -6;
        break;
    case 6: tzTarget = TimeZone.getTimeZone("America/New_York");
        offset = -5;
        break;
}
//converting from GMT to US timezones based on offset and dst
cal.setTimeZone(tzTarget);
dst = tzTarget.getDSTSavings();
dst = dst/3600000;
offset = offset + dst;
cal.add(Calendar.HOUR, offset);

Date date = cal.getTime();

System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

}

【问题讨论】:

  • 你试过的代码在哪里?
  • 所以你想把它转换成CDT?

标签: java timezone dst


【解决方案1】:

要将给定的日期转换为不同的时区,您需要创建具有适当时区的日期格式化程序。日期实例只是相对于纪元的长值;它没有时区信息。所以我们没有将它转换为不同的时区,我们只是在不同的时区中表示它。这就是为什么当我们想要创建日期实例的字符串表示时需要时区信息的原因。

这里有一些代码来说明上述内容。我刚刚将时区添加到您的日期格式字符串中以使事情清楚。

/*
 * Converts a specified time to different time zones
 */
public void convert(Date dt) {
    // This prints: Date with default formatter: 2013-03-14 22:00:12 PDT
    // As my machine is in PDT time zone
    System.out.println("Date with default formatter: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(dt));

    // This prints: Date with IST time zone formatter: 2013-03-15 10:30:12 GMT+05:30
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    TimeZone tz = TimeZone.getTimeZone("GMT+0530");
    sdf.setTimeZone(tz);
    String dateIST = sdf.format(dt);
    System.out.println("Date with IST time zone formatter: " + dateIST);

    // This prints: Date CST time zone formatter: 2013-03-15 00:00:12 CDT        
    tz = TimeZone.getTimeZone("CST");
    sdf.setTimeZone(tz);
    System.out.println("Date CST time zone formatter: " + sdf.format(dt));
}

我认为这就是您想要做的 - 将给定时间转换为不同的时区。为此,我认为您不需要添加/减去任何偏移量,因为您只希望 same 时间表示在不同的时区,TimeZone 实例应该能够处理在格式化期间。

至于夏令时,TimeZone 也应该能够处理这个问题。如果您在我的示例代码中注意到,我使用CST 创建TimeZone 实例,CST 是“GMT -06 小时”。但它给出的输出是CDT,即“GMT -05 hours”,因为这个时区实例使用夏令时。因此,通过使用适当的时区,您也应该能够处理夏令时。

【讨论】:

  • public void convert(Date dt){ Calendar cal = Calendar.getInstance(); cal.setTime(dt); // 在 IST 中获取时间 long millis = cal.getTimeInMillis(); GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("America/Chicago"),Locale.US); calendar.setTimeInMillis(millis);日期日期 = calendar.getTime(); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));} 这就是我试过的。但我得到的输出与我给出的输入相同的日期时间...... !!!
  • @akhilgm89,您将获得相同的日期字符串,因为您正在使用具有默认时区的SimpleDateFormat 实例,这将是您系统的时区。 Date 实例只是一个长值,表示自纪元以来的毫秒数;它不包含时区信息。创建字符串表示时,您需要指定要使用的时区。我已经编辑了我的帖子以用代码解释这一点。看看吧。
【解决方案2】:

java.util.GregorianCalendar 允许您创建带有时区的日期。不幸的是,加法和减法从那里很糟糕。 (How do you subtract Dates in Java?)

由于您在两个时区之间进行转换,您还可以使用java.util.TimeZone 并使用tz1.getOffset(date) - tz2.getOffset(date) 的差异。做减法时注意顺序。

【讨论】:

    【解决方案3】:

    乔达时间

    使用Joda-Time 使这变得更容易。或者尝试 Java 8 中的新 java.time 包。

    这是一些使用 Joda-Time 2.3 的示例代码。在 StackOverflow 中搜索更多示例。

    印度时间……

    DateTimeZone timeZone_India = DateTimeZone.forID( "Asia/Kolkata" );
    DateTime dateTimeIndia = new DateTime( date, timeZone_India );
    

    正在调整与纽约时间相同的显示时刻……

    DateTimeZone timeZone_NewYork = DateTimeZone.forID( "America/New_York" );
    DateTime dateTimeNewYork = dateTimeIndia.withZone( timeZone_NewYork ); // Same moment, different wall-clock time.
    

    仍然是同一时刻,但在UTC(无时区偏移)。

    DateTime dateTimeUtc = dateTimeIndia.withZone( DateTimeZone.UTC );
    

    使用正确的时区名称

    避免使用 3-4 个字母的时区代码。它们既不是标准化的,也不是唯一的。例如,您的IST 可以表示爱尔兰标准时间或印度标准时间。使用proper time zone names

    【讨论】:

      猜你喜欢
      • 2014-06-01
      • 2011-12-14
      • 2021-08-02
      • 2015-08-04
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      相关资源
      最近更新 更多