Java中的IST到EST时间转换

Java中的IST到EST时间转换,java,datetime,datetimeoffset,Java,Datetime,Datetimeoffset,我在Java中的IST Time中有一个日期字段。我想将其转换为EST时间,并且输出应仅作为日期类型。我可以使用下面的代码实现同样的功能:- SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); Date date = new Date(); DateForma

我在Java中的IST Time中有一个日期字段。我想将其转换为EST时间,并且输出应仅作为日期类型。我可以使用下面的代码实现同样的功能:-

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);
上述代码的问题在于,尽管日期转换为EST时间,但时区仍显示为IST而非EST。日期的其余部分转换得非常好。是否有任何方法可以在日期字段中将时区显式设置为EST。对此,我们将非常感谢您的帮助


-Subhadeep日期类是时区不可知的。基本上,它始终基于GMT,尽管打印时使用当前系统时区进行调整

但是,
日历
是特定于时区的。看

考虑:

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

您是否应该在
SimpleDataFormat
模式中为时区模式添加
z

因此,它应该是
DateFormat timeFormat=newsimpledateformat(“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)
}
在最后一个打印语句中,当前日期格式使用东部夏令时(EDT时区)打印。可能是因为。

正确的解释是java.util.Date似乎有时区,但没有。它的
toString
方法在生成字符串表示时应用JVM的默认时区

这是避免java绑定java.util.Date和.Calendar类的众多原因之一。避开它们。取而代之的是使用java 8中内置的java.time包(受JSR 310定义的Joda time启发)

下面是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
印度日期时间:2014-01-02T12:34:56.000+05:30
纽约时间:2014-01-02T02:04:56.000-05:00
日期时间UTC:2014-01-02T07:04:56.000Z
java.time 旧式日期时间API(
java.util
date-time类型及其格式类型,
SimpleDateFormat
等)已过时且容易出错。建议完全停止使用它,并切换到
java.time
,即*

使用
java.time
的解决方案,现代API: 输出:

2021-05-19T21:08:54.241341Z
2021-05-20T02:38:54.241341+05:30[Asia/Calcutta]
2021-05-19T17:08:54.241341-04:00[America/New_York]
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS[zzzzz]");

        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
        String dtIndia = sdf.format(date);

        sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        String dtNewYork = sdf.format(date);

        System.out.println(dtIndia);
        System.out.println(dtNewYork);
    }
}
Wed May 19 22:16:08 BST 2021
2021-05-20T02:46:08.024[India Standard Time]
2021-05-19T17:16:08.024[Eastern Daylight Time]
表示时间线上的瞬时点。输出中的
Z
为零时区偏移。它代表Zulu并指定
Etc/UTC
时区(其时区偏移量为
+00:00
小时)

了解更多关于
java.time
、来自的*

使用传统API的解决方案:
java.util.Date
对象不像;相反,它表示自称为“历元”的标准基准时间(即1970年1月1日00:00:00 GMT)起的毫秒数。当您打印
java.util.Date
的对象时,其
toString
方法返回JVM时区中的日期时间,从该毫秒值计算得出。如果需要在不同的时区中打印日期时间,则需要将时区设置为
SimpleDateFormat
,并从中获取格式化字符串

演示:

2021-05-19T21:08:54.241341Z
2021-05-20T02:38:54.241341+05:30[Asia/Calcutta]
2021-05-19T17:08:54.241341-04:00[America/New_York]
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS[zzzzz]");

        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
        String dtIndia = sdf.format(date);

        sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        String dtNewYork = sdf.format(date);

        System.out.println(dtIndia);
        System.out.println(dtNewYork);
    }
}
Wed May 19 22:16:08 BST 2021
2021-05-20T02:46:08.024[India Standard Time]
2021-05-19T17:16:08.024[Eastern Daylight Time]
输出:

2021-05-19T21:08:54.241341Z
2021-05-20T02:38:54.241341+05:30[Asia/Calcutta]
2021-05-19T17:08:54.241341-04:00[America/New_York]
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS[zzzzz]");

        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
        String dtIndia = sdf.format(date);

        sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        String dtNewYork = sdf.format(date);

        System.out.println(dtIndia);
        System.out.println(dtNewYork);
    }
}
Wed May 19 22:16:08 BST 2021
2021-05-20T02:46:08.024[India Standard Time]
2021-05-19T17:16:08.024[Eastern Daylight Time]

*无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。

dateTimeFormat
是代码中未使用的变量。不使用Z参数。现在日期的其余部分也在IST中:(即使使用小写(
z
)?这是因为大写(
z
)只是相对于GMT的打印时间,如
-0400
。仅使用小z尝试…在第一次回复中键入:(您使用的是框架吗?对不起,我想不出多少。我上面的代码是通过创建一个简单的java应用程序来测试的。@WayanWiprayoga如何从中获取EDT时区日期对象?