有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将EST中的字符串转换为PST中的日期

我尝试了以下方法,并在Web上搜索,以找到解决方案,但没有成功:寻找将IST中的字符串转换为PST的解决方案:

String string = new Date().toString();
        System.out.println(string);
        SimpleDateFormat dt = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        dt.setTimeZone(TimeZone.getTimeZone("PST"));
        Date D = dt.parse(string);
        System.out.println(""+ D);

即使我把时区设为PST,我也能看到输入IST 下面是我的建议:

2017年4月18日星期二18:58:09 2017年4月18日星期二18:58:09

我在这里尝试了另一个选项,我看到它在PST中显示时间,但我看到下面的输出有点混乱:

public static Date convertFromOneTimeZoneToOhter(Date dt,String from,String to ) {

          TimeZone fromTimezone =TimeZone.getTimeZone(from);//get Timezone object
          TimeZone toTimezone=TimeZone.getTimeZone(to);

          long fromOffset = fromTimezone.getOffset(dt.getTime());//get offset
          long toOffset = toTimezone.getOffset(dt.getTime());

          //calculate offset difference and calculate the actual time
          long convertedTime = dt.getTime() - (fromOffset - toOffset);
          Date d2 = new Date(convertedTime);

          return d2;
    }

输出: 转换日期:2017年4月18日星期二06:28:09

有人能帮我一下吗:我找到了很多将IST日期时间转换为PST字符串的解决方案,但没有将IST/EST日期转换为PST日期。 正如我上面提到的,我们可以格式化为字符串,但我正在寻找一个转换回日期的示例


共 (2) 个答案

  1. # 1 楼答案

    因为这里的一些读者将使用Java 8或更高版本,而一些读者将使用Java 7或更早版本,所以我将同时讨论这两种版本

    如果可以,我建议您使用Java 8中引入的java.time类:

        ZoneId targetTz = ZoneId.of("America/Los_Angeles");
    
        String string = "Tue Apr 18 18:58:09 +0300 2017";
        DateTimeFormatter format = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss ZZZ uuuu",
                Locale.ENGLISH);
        ZonedDateTime sourceTime = ZonedDateTime.parse(string, format);
        ZonedDateTime targetTime = sourceTime.withZoneSameInstant(targetTz);
        String result = targetTime.format(format);
        System.out.println(result);
    

    这张照片是:

    Tue Apr 18 08:58:09 -0700 2017
    

    你说你想要一个PST日期,而这正是ZonedDateTime给你的:带有时区信息的日期和时间

    在这个例子中,我在字符串中给出了一个区域偏移量+0300(对应于以色列夏时制)。我明白,时区的设定对你来说并不重要。我想避免使用三个字母和四个字母的时区缩写,比如IST。不仅指爱尔兰标准时间、以色列标准时间或印度标准时间。我还注意到java.time类选择18:58:09作为18:58:09 IDT(UTC+3),因为它知道以色列在4月18日的DST上;我在下面返回的SimpleDateFormat从字面上理解IST,并将18:58:09 IST解释为18:58:09+0200,这是UTC中1小时后的时间

    如果需要,可以在Java 1.7中使用java.time类。你可以在the ThreeTen Backport中找到它们

    如果您不想使用java.time,那么使用Java 1.0和1.1中过时的类的方法在本例中没有太大不同,只是我无法给出您要求的PST日期:

        TimeZone targetTz = TimeZone.getTimeZone("America/Los_Angeles");
        String string = "Tue Apr 18 18:58:09 +0300 2017";
        SimpleDateFormat dt = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ENGLISH);
        Date d = dt.parse(string);
        dt.setTimeZone(targetTz);
        String result = dt.format(d);
        System.out.println(result);
    

    它打印的结果与上面相同。但是,您注意到只有一个Date对象。一个Date对象不能保存任何时区信息,因此如果需要,您必须将targetTz对象与d一起带来。在Date对象中应该有一个时区,这是一个常见的误解,可能是因为它的toString()打印了一个时区。这始终是JVM的默认时区,而不是来自Date对象

  2. # 2 楼答案

    您应该研究Java8的新日期API,它直接处理时区

    // Get the current date and time
          ZonedDateTime date1 = ZonedDateTime.parse("2007-12-03T10:15:30+05:30[Asia/Karachi]");
          System.out.println("date1: " + date1);
    
          ZonedDateTime zonedDateTime = ZonedDateTime.now();
          System.out.println("Zoned Date Time: " + zonedDateTime);
    
          ZoneId id = ZoneId.of("Europe/Paris");
          System.out.println("ZoneId: " + id);
    
          ZoneId currentZone = ZoneId.systemDefault();
          System.out.println("CurrentZone: " + currentZone);
    

    印刷品:

    date1: 2007-12-03T10:15:30+05:00[Asia/Karachi]
    Zoned Date Time: 2017-04-18T09:52:09.045-04:00[America/New_York]
    ZoneId: Europe/Paris
    CurrentZone: America/New_York