2013-03-18 20 views
0

我在IST时间在Java中有一个Date字段。我想将其转换为EST时间,并且输出应仅作为日期类型。我可以使用下面的代码来完成相同的: -IST到EST在Java中的时间转换

SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); 
Date date = new Date(); 
DateFormat timeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
timeFormat.setTimeZone(TimeZone.getTimeZone("America/New_York")); 
String estTime = timeFormat.format(date); 
date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(estTime); 

与上面这段代码的问题是,虽然日期是美国东部时间时间转换,时区仍然显示为IST和而不是EST。日期的其余部分转换得很好。有没有什么办法可以明确地设置日期字段中的时区到EST。任何有关这方面的帮助将不胜感激。

-Subhadeep

+0

'dateTimeFormat'是在代码中未使用的变量。 – 2013-03-18 12:42:53

回答

1

Date类是时区无关。基本上,它始终基于GMT,但打印时使用当前系统时区进行调整。

但是,Calendar是时区特定的。见Calendar.setTimeZone()

考虑:

Calendar cal = new GregorianCalendar(); 
    cal.setTimeZone(TimeZone.getTimeZone("America/New_York")); 
    cal.setTime(new Date()); 
0

如果您在SimpleDateFormat模式添加z的时区模式?

所以,它应该是DateFormat timeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z")。我改变了你这样的代码:

public static void main(String[] args) throws ParseException { 
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z"); 
    dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); 
    Date date = new Date(); 

    System.out.println(dateTimeFormat.format(date)); // this print IST Timezone 

    DateFormat timeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z"); 
    timeFormat.setTimeZone(TimeZone.getTimeZone("America/New_York")); 

    String estTime = timeFormat.format(date); 
    date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z", Locale.ENGLISH).parse(estTime); 

    System.out.println(timeFormat.format(date)); // this print EDT Timezone currently (on March) 
} 

在过去的print语句,当前的日期格式打印EDT时区(东部夏令时间)。也许是因为this

+0

不能使用Z参数。日期的其余部分现在也在IST中:( – user2182350 2013-03-18 13:24:38

+0

即使小写('z')?这是因为大写字母('Z')只是打印相对GMT时间的相对时间,例如'-0400'。 – 2013-03-18 13:31:45

+0

尝试过与小z只...在第一个回复中的错字:( – user2182350 2013-03-18 13:38:15

0

正确的answer by John B解释说java.util.Date似乎有一个时区,但没有。它的toString方法在生成字符串表示时应用您的JVM的默认时区。

这是避免与Java捆绑在一起的java.util.Date和.Calendar类的很多原因之一。避免它们。取而代之的是使用Joda-Time或Java 8中内置的java.time包(受Joda-Time的启发,由JSR 310定义)。

以下是Joda-Time 2.3中的一些示例代码。

String input = "01/02/2014 12:34:56"; 
DateTimeFormatter formatterInput = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss"); 
DateTimeZone timeZoneIndia = DateTimeZone.forID("Asia/Kolkata"); 
DateTime dateTimeIndia = formatterInput.withZone(timeZoneIndia).parseDateTime(input); 

DateTimeZone timeZoneNewYork = DateTimeZone.forID("America/New_York"); 
DateTime dateTimeNewYork = dateTimeIndia.withZone(timeZoneNewYork); 

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

转储到控制台...

System.out.println("input: " + input); 
System.out.println("dateTimeIndia: " + dateTimeIndia); 
System.out.println("dateTimeNewYork: " + dateTimeNewYork); 
System.out.println("dateTimeUtc: " + dateTimeUtc); 

当运行...

input: 01/02/2014 12:34:56 
dateTimeIndia: 2014-01-02T12:34:56.000+05:30 
dateTimeNewYork: 2014-01-02T02:04:56.000-05:00 
dateTimeUtc: 2014-01-02T07:04:56.000Z 
相关问题