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
356 views
in Technique[技术] by (71.8m points)

java - how convert date" formate including IST

I have string date "Tue May 07 15:41:50 IST 2019" want to convert into date but its not working mention below formats.

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.US/Locale/English both);
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
date date = formatter.parse("Tue May 07 15:41:50 IST 2019");

When I am trying to parse IST using z or zzz or Z or ZZZ but not success in android.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Normally, using z would give you the correct timezone, here with HongKong :

System.out.println(
    new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy z", Locale.ENGLISH)
        .parse("Tue May 07 15:41:50 2019 HKT")
);

Tue May 07 09:41:50 CEST 2019

Documentation

But like it is explained in TimeZone, a "Three-letter time zone IDs" is not unique and this is the reason it should not be used.

[...] some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

Proof of concept

We can check that IST represents more than one timezone with this dirty code to get every Timezone matching the ZoneId of every "IST" zone supported by the SimpleDateFormatter:

String shortZoneID = "IST";
Arrays.stream(
            new SimpleDateFormat().getDateFormatSymbols().getZoneStrings())
            .filter(s -> shortZoneID.equals(s[2]) || shortZoneID.equals(s[4]))
            .map(s -> TimeZone.getTimeZone(s[0]))
            .map(t -> String.format("%-50s %s", t.getDisplayName(), Duration.of(t.getRawOffset(), ChronoUnit.MILLIS)))
            .distinct()
            .forEach(System.out::println)
            ;

Output :

Israel Standard Time                               PT2H  
India Standard Time                                PT5H30M  
Greenwich Mean Time                                PT0S  

Explaining why IST" seems to be in GMT.

System.out.println(
    new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy z", Locale.ENGLISH)
        .parse("Tue May 07 15:41:50 2019 IST")
);

Tue May 07 15:41:50 CEST 2019

Not sure yet how Java prioritize the timezone in this case, does it take the first found, or is it based on the current locale (I am on GMT+01:00 timezone)?
But even with this information, the result might not be correct.

Solutions

In the meantime, here are some correct form of recovering a Timezone :

Using the offset

System.out.println(
        new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy XXX", Locale.ENGLISH)
            .parse("Tue May 07 15:41:50 2019 +05:30"));

Using the full name of the zone

System.out.println(
        new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy zzzz", Locale.ENGLISH)
            .parse("Tue May 07 15:41:50 2019 India Standard Time"));

Both output :

Tue May 07 12:11:50 CEST 2019


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

57.0k users

...