Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java DateFormatter未以给定格式打印值_Java_Date_Datetime_Jodatime_Datetime Format - Fatal编程技术网

Java DateFormatter未以给定格式打印值

Java DateFormatter未以给定格式打印值,java,date,datetime,jodatime,datetime-format,Java,Date,Datetime,Jodatime,Datetime Format,我的日期时间格式字符串是:yyyy-MM-dd'T'HH:MM:ss.SSSZ 我正在使用Joda Time的DateTimeFormatter以上述格式打印我的日期 现在,考虑日期为 2016/04/01 23:00:00 那么它应该已经打印出来了 2016-04-01T23:00:00.000Z 但是,它打印出来了 2016-04-01T23:00:00.000+0200 请帮助我以字符串格式中指定的相同格式打印日期。根据Z有特殊含义: Z zone-offset 如果要用“'

我的日期时间格式字符串是:
yyyy-MM-dd'T'HH:MM:ss.SSSZ

我正在使用Joda Time的
DateTimeFormatter
以上述格式打印我的日期

现在,考虑日期为

2016/04/01 23:00:00

那么它应该已经打印出来了

2016-04-01T23:00:00.000Z

但是,它打印出来了

2016-04-01T23:00:00.000+0200

请帮助我以字符串格式中指定的相同格式打印日期。

根据Z有特殊含义:

Z       zone-offset
如果要用“'”转义Z引号Z:

yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
例如:

java.time.LocalDateTime date = java.time.LocalDateTime.now();
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
java.lang.String text = date.format(formatter);
System.out.println(text);
印刷品

2016-06-11T18:39:41.962Z
因此,Z具有特殊含义:

Z       zone-offset
如果要用“'”转义Z引号Z:

yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
例如:

java.time.LocalDateTime date = java.time.LocalDateTime.now();
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
java.lang.String text = date.format(formatter);
System.out.println(text);
印刷品

2016-06-11T18:39:41.962Z
2016-04-01T23:00:00.000Z!=2016-04-01T23:00:00.000+0200 以下是巴兹尔·布尔克对以下问题的评论:


不,不,不。你所做的只是附加文本,制造虚假。如果 你的约会时间代表时区中两小时的一个时刻 在UTC之前,例如
欧洲/赫尔辛基
,然后在末尾打一个
Z
意思是UTC,你现在在说谎,代表 关闭两小时的值。这就像更换美元符号一样 带有欧元货币符号的价格,但未能改变价格 号码

为了说明他所提到的:

£100 != $100
2016-04-01T23:00:00.000Z
中的
Z
是零时区偏移的值。它代表Zulu并指定
Etc/UTC
时区(其时区偏移量为
+00:00
小时)。同一时刻将在不同的时区以不同的值呈现,例如

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.parse("2016-04-01T23:00:00.000Z");

        ZonedDateTime zdtNewYork = instant.atZone(ZoneId.of("America/New_York"));
        ZonedDateTime zdtIndia = instant.atZone(ZoneId.of("Asia/Kolkata"));
        ZonedDateTime zdtNepal = instant.atZone(ZoneId.of("Asia/Kathmandu"));

        System.out.println(zdtNewYork);
        System.out.println(zdtIndia);
        System.out.println(zdtNepal);

        // Or at a fixed timezone offset of +02:00 hours
        OffsetDateTime odtWithTwoHoursOffset = instant.atOffset(ZoneOffset.of("+02:00"));
        System.out.println(odtWithTwoHoursOffset);
    }
}
输出:

2016-04-01T19:00-04:00[America/New_York]
2016-04-02T04:30+05:30[Asia/Kolkata]
2016-04-02T04:45+05:45[Asia/Kathmandu]
2016-04-02T01:00+02:00
2016-04-01T23:00:00Z
2016-04-01T23:00Z[Etc/UTC]
2016-04-01T23:00Z[Etc/UTC]

2016-04-01T23:00:00Z
2016-04-01T23:00Z
2016-04-01T23:00Z
为了进一步理解这个概念,请尝试将日期时间从一个时区转换为另一个时区,例如,我展示了将纽约日期时间转换为UTC的过程。我展示了另一种将时区偏移量为
+02:00
小时的日期时间转换为UTC的方法

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        // #######Example of converting a date-time from one timezone to another#####
        ZonedDateTime zdtNewYork = ZonedDateTime.parse("2016-04-01T19:00-04:00[America/New_York]");

        Instant instant = zdtNewYork.toInstant();
        System.out.println(instant);

        // Or as ZonedDateTime
        ZonedDateTime zdtUtc = zdtNewYork.withZoneSameInstant(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc);

        // Alternatively, this can be obtained from instant
        zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc);
        // ###########################################################################

        System.out.println();

        // #####Example of converting a date-time at a fixed timezone offset to UTC###
        OffsetDateTime odtNewYork = OffsetDateTime.parse("2016-04-02T01:00+02:00");

        instant = odtNewYork.toInstant();
        System.out.println(instant);

        // Alternatively
        OffsetDateTime odtUtc = odtNewYork.withOffsetSameInstant(ZoneOffset.UTC);
        System.out.println(odtUtc);

        // Alternatively,
        odtUtc = instant.atOffset(ZoneOffset.UTC);
        System.out.println(odtUtc);
        // ###########################################################################
    }
}
输出:

2016-04-01T19:00-04:00[America/New_York]
2016-04-02T04:30+05:30[Asia/Kolkata]
2016-04-02T04:45+05:45[Asia/Kathmandu]
2016-04-02T01:00+02:00
2016-04-01T23:00:00Z
2016-04-01T23:00Z[Etc/UTC]
2016-04-01T23:00Z[Etc/UTC]

2016-04-01T23:00:00Z
2016-04-01T23:00Z
2016-04-01T23:00Z
2016-04-01T23:00:00.000Z!=2016-04-01T23:00:00.000+0200 以下是巴兹尔·布尔克对以下问题的评论:


不,不,不。你所做的只是附加文本,制造虚假。如果 你的约会时间代表时区中两小时的一个时刻 在UTC之前,例如
欧洲/赫尔辛基
,然后在末尾打一个
Z
意思是UTC,你现在在说谎,代表 关闭两小时的值。这就像更换美元符号一样 带有欧元货币符号的价格,但未能改变价格 号码

为了说明他所提到的:

£100 != $100
2016-04-01T23:00:00.000Z
中的
Z
是零时区偏移的值。它代表Zulu并指定
Etc/UTC
时区(其时区偏移量为
+00:00
小时)。同一时刻将在不同的时区以不同的值呈现,例如

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.parse("2016-04-01T23:00:00.000Z");

        ZonedDateTime zdtNewYork = instant.atZone(ZoneId.of("America/New_York"));
        ZonedDateTime zdtIndia = instant.atZone(ZoneId.of("Asia/Kolkata"));
        ZonedDateTime zdtNepal = instant.atZone(ZoneId.of("Asia/Kathmandu"));

        System.out.println(zdtNewYork);
        System.out.println(zdtIndia);
        System.out.println(zdtNepal);

        // Or at a fixed timezone offset of +02:00 hours
        OffsetDateTime odtWithTwoHoursOffset = instant.atOffset(ZoneOffset.of("+02:00"));
        System.out.println(odtWithTwoHoursOffset);
    }
}
输出:

2016-04-01T19:00-04:00[America/New_York]
2016-04-02T04:30+05:30[Asia/Kolkata]
2016-04-02T04:45+05:45[Asia/Kathmandu]
2016-04-02T01:00+02:00
2016-04-01T23:00:00Z
2016-04-01T23:00Z[Etc/UTC]
2016-04-01T23:00Z[Etc/UTC]

2016-04-01T23:00:00Z
2016-04-01T23:00Z
2016-04-01T23:00Z
为了进一步理解这个概念,请尝试将日期时间从一个时区转换为另一个时区,例如,我展示了将纽约日期时间转换为UTC的过程。我展示了另一种将时区偏移量为
+02:00
小时的日期时间转换为UTC的方法

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        // #######Example of converting a date-time from one timezone to another#####
        ZonedDateTime zdtNewYork = ZonedDateTime.parse("2016-04-01T19:00-04:00[America/New_York]");

        Instant instant = zdtNewYork.toInstant();
        System.out.println(instant);

        // Or as ZonedDateTime
        ZonedDateTime zdtUtc = zdtNewYork.withZoneSameInstant(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc);

        // Alternatively, this can be obtained from instant
        zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc);
        // ###########################################################################

        System.out.println();

        // #####Example of converting a date-time at a fixed timezone offset to UTC###
        OffsetDateTime odtNewYork = OffsetDateTime.parse("2016-04-02T01:00+02:00");

        instant = odtNewYork.toInstant();
        System.out.println(instant);

        // Alternatively
        OffsetDateTime odtUtc = odtNewYork.withOffsetSameInstant(ZoneOffset.UTC);
        System.out.println(odtUtc);

        // Alternatively,
        odtUtc = instant.atOffset(ZoneOffset.UTC);
        System.out.println(odtUtc);
        // ###########################################################################
    }
}
输出:

2016-04-01T19:00-04:00[America/New_York]
2016-04-02T04:30+05:30[Asia/Kolkata]
2016-04-02T04:45+05:45[Asia/Kathmandu]
2016-04-02T01:00+02:00
2016-04-01T23:00:00Z
2016-04-01T23:00Z[Etc/UTC]
2016-04-01T23:00Z[Etc/UTC]

2016-04-01T23:00:00Z
2016-04-01T23:00Z
2016-04-01T23:00Z

请提供生成此行为的完整代码段请提供生成此行为的完整代码段非常感谢@AnatolyS。我遗漏了Z的单引号。不,不,不。你所做的只是附加文本,创造了一个虚假。如果您的日期时间表示时区中比UTC早两小时的某一时刻,例如
欧洲/赫尔辛基
,并且您在末尾打了一个
Z
,表示
祖鲁
,表示UTC,那么您现在在撒谎,表示两小时后的值。这就像用欧元货币符号替换价格中的美元符号,但没有改变数字。非常感谢@AnatolyS。我遗漏了Z的单引号。不,不,不。你所做的只是附加文本,创造了一个虚假。如果您的日期时间表示时区中比UTC早两小时的某一时刻,例如
欧洲/赫尔辛基
,并且您在末尾打了一个
Z
,表示
祖鲁
,表示UTC,那么您现在在撒谎,表示两小时后的值。这就像用欧元货币符号替换价格中的美元符号,但没有改变数字。