在Java8中将日期转换为EST

在Java8中将日期转换为EST,java,Java,我试图将日期转换为以下时区,但结果与预期不符-我得到的要求是,例如,从PMST转换为EST输出应少于2小时 PMST, NST, AST, 美国东部时间, 科技委, MST, 太平洋标准时间, 阿克斯特, HAST String inputDate = "2017/04/30 08:10"; DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm"); LocalDateTime loca

我试图将日期转换为以下时区,但结果与预期不符-我得到的要求是,例如,从PMST转换为EST输出应少于2小时

PMST, NST, AST, 美国东部时间, 科技委, MST, 太平洋标准时间, 阿克斯特, HAST

String inputDate = "2017/04/30 08:10";
DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
LocalDateTime local = LocalDateTime.parse(inputDate, sourceFormatter);
ZonedDateTime zoned = local.atZone(TimeZone.getTimeZone("PMST").toZoneId());
ZonedDateTime requiredZone = zoned.withZoneSameInstant(TimeZone.getTimeZone("EST").toZoneId());
System.out.println(requiredZone);
输出-2017-04-30T03:10-05:00


所以您知道您输入的“本地日期”位于PMST时区

String localDateTimeString = "2017/04/30 08:10";
但是你应该注意到上面没有你知道的时区信息。因此,您需要将该信息添加到字符串中

但是我们现在只有一个
字符串
,所以首先我们必须决定我们将使用的日期-时间格式,并且它必须具有时区意识

DateTimeFormatter dateTimeWithZoneFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm Z");
这里,
Z
表示时区偏移。我们知道,
PMST
UTC-0300
,因此我们需要将此信息添加到本地时间字符串中

String dateTimeString = "2017/04/30 08:10" + " -0300";
现在,我们可以使用日期时间格式化程序读取它

ZonedDateTime dateTimeInPMST = ZonedDateTime.parse(dateTimeString, dateTimeWithZoneFormatter);
现在我们可以得到任何时区的日期

ZonedDateTime dateTimeInEST = dateTimeInPMST.withZoneSameInstant(TimeZone.getTimeZone("EST").toZoneId());
编辑1::

获取时区的偏移量(比如
EST

请注意:在
-05:30
中,我们必须更改
日期时间格式以适应此情况

DateTimeFormatter dateTimeWithZoneFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm ZZZ");
现在,您可以将其添加到任何dateTimeString

public String makeOffsetAwareDateTimeString(String dateTimeString, String timezone) {
    int offsetInSeconds = TimeZone.getTimeZone(timezone).getRawOffset() / 1000;
    ZoneOffset zoneOffset = ZoneOffset.ofTotalSeconds(offsetInSeconds);
    String zoneOffsetString = zoneOffset.toString();
    return dateTimeString + " " + zoneOffsetString;
}

这对我有用。修改需要的区域

public class ZonedDateTime {

    private static final String DATE_FORMAT = "dd/M/yyyy hh:mm:ss a";

    public static void main(String[] args) {

        String dateInString = "26/12/2017 10:15:55 AM";
        LocalDateTime ldt = LocalDateTime.parse(dateInString, DateTimeFormatter.ofPattern(DATE_FORMAT));

        ZoneId newYokZoneId = ZoneId.of("America/New_York");
        ZonedDateTime nyDateTime =ldt.atZone(newYokZoneId);
        System.out.println("Date (New York) : " + nyDateTime);

        DateTimeFormatter format = DateTimeFormatter.ofPattern(DATE_FORMAT);
        System.out.println("Date (New York) : " + format.format(nyDateTime));

    }

}
使用此工具查找所有区域

TreeSet<String> sortedZones = new TreeSet<>(ZoneId.getAvailableZoneIds());

    for (String zone : sortedZones) {
            System.out.println(zone);
    }
TreeSet sortedZones=新树集(ZoneId.getAvailableZoneId());
用于(字符串区域:sortedZones){
系统输出打印LN(区域);
}
避免伪区域 切勿使用媒体中常见的3-4个字母的缩写,如
CST
IST
EST
。这些时区不是真正的时区,没有标准化,甚至不是唯一的(!)

真实时区:
大陆/地区
相反,确定你的真实时区。以
大陆/地区
的格式,如
美国/蒙特利尔
非洲/卡萨布兰卡
太平洋/奥克兰

更改输入以符合ISO 8601标准格式

String input = "2017/04/30 08:10".replace( " " , "T" ) ;
解析为
LocalDateTime
,因为您的输入缺少与UTC或时区的偏移量指示符

LocalDateTime ldt = LocalDateTime.parse( input ) ;
如果您确定时区用于此输入,请应用时区

ZoneId z = ZoneId.of( "America/New_York" ) ;
ZonedDateTime zdt = ldt.withZoneSameInstant( z ) ;

正如Sarvesh所提到的,ZoneId中没有提到时区PMST,所以您必须明确提到它。您可以在zoneId.javaIdentify中检查支持的zoneId,或更正您的
PMST
。谢谢:)sarvesh,您能告诉我如何获取其他zoneId的时区偏移吗?PMST是UTC-0300添加了获取时区偏移的方法。
ZoneId z = ZoneId.of( "America/New_York" ) ;
ZonedDateTime zdt = ldt.withZoneSameInstant( z ) ;