2013-03-14 44 views
0

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

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

}

+0

你试过的代码在哪里? – Apurv 2013-03-14 11:56:01

+0

那么你想将它转换为CDT? – 2013-03-14 12:00:58

回答

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

我认为这是你正在做什么 - 一个给定的时间转换为不同的时区。为此,我认为您不需要添加/减去任何偏移量,因为您只需要在不同的时区中显示相同的时间,并且TimeZone实例应该能够在格式化过程中处理该时间。

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

+0

public void convert(Date dt){日历dt} {日历cal = Calendar.getInstance(); cal.setTime(dt); //在IST中获取时间 \t \t long millis = cal.getTimeInMillis(); GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone(“America/Chicago”),Locale.US); calendar.setTimeInMillis(millis); \t \t Date date = calendar.getTime(); (新的SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(date));}这是我试过的。但是我得到的输出与我给出的输入一样... !!! – akhilgm 2013-03-14 13:23:17

+0

@ akhilgm89,你得到相同的日期字符串,因为你正在使用具有默认时区的SimpleDateFormat实例,这将是你系统的时区。 “日期”实例只是一个很长的值,表示自纪元以来的毫秒数;它不包含时区信息。当您创建字符串表示形式时,您需要指定要使用的时区。我编辑了我的帖子,用代码解释了这一点。看一看。 – Kshitij 2013-03-15 05:49:41

+0

它工作..... !!!!!! thnx很多.... :) – akhilgm 2013-03-15 06:21:11

0

乔达时间

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

相关问题