Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

java - DateFormatter is not printing value in the given format

My date-time format string is : yyyy-MM-dd'T'HH:mm:ss.SSSZ
I am using DateTimeFormatter from Joda Time to print my date in the above mentioned format.

Now, consider the date as

2016/04/01 23:00:00

then it should have printed

2016-04-01T23:00:00.000Z

But, it prints

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

Please help me in getting the date printed in the same format as specified in the string format.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

2016-04-01T23:00:00.000Z != 2016-04-01T23:00:00.000+0200

Here is what Basil Bourque has commented on the wrong answer:

No, no, no. All you have done is append text, creating a falsity. If your date-time represents a moment in a time zone that is two hours ahead of UTC such as Europe/Helsinki, and you slap a Z on the end which says Zulu and means UTC, you are now telling a lie, representing value that is off by two hours. This is like replacing the dollar sign in a price with a Euro currency symbol but failing to change the number.

Just to illustrate what he has mentioned:

£100 != $100

The Z in 2016-04-01T23:00:00.000Z is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours). The same moment will be presented in different timezones with different values e.g.

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);
    }
}

Output:

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

To understand this concept a bit further, try converting a date-time from one timezone to another e.g. I have shown a conversion of a New York date-time into UTC. I have shown another conversion of a date-time with a timezone offset of +02:00 hours into 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);
        // ###########################################################################
    }
}

Output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...