1
votes

I have an linux application that presents time with time-zone in iOS-XR format: tech system clock status local-time-string "10:16:12.523 CDT Thu Mar 21 2013"

I also see that the "CDT" shortcut is being used in the linux "zdump" output:

zdump -v America/Havana | grep 2013 America/Havana Sun Mar 10 04:59:59 2013 UTC = Sat Mar 9 23:59:59 2013 CST isdst=0 gmtoff=-18000 America/Havana Sun Mar 10 05:00:00 2013 UTC = Sun Mar 10 01:00:00 2013 CDT isdst=1 gmtoff=-14400 America/Havana Sun Nov 3 04:59:59 2013 UTC = Sun Nov 3 00:59:59 2013 CDT isdst=1 gmtoff=-14400 America/Havana Sun Nov 3 05:00:00 2013 UTC = Sun Nov 3 00:00:00 2013 CST isdst=0 gmtoff=-18000

but when I run "System.out.println(TimeZone.getTimeZone("CDT"));" Java command I get the GMT time zone...

  1. can anybody explain what is the criteria for the short-cuts existence in the Java timeZone object?
  2. where can I find an open-source code for converting time strings into "Date" objects... the current parser I'm using relays on Java Calendar, SimpleDateFormat and Date but since Java TimeZone does not recognize "CDT" it parses this string as it was a GMT time...
3

3 Answers

1
votes

1) see java.util.DateFormatSymbols.getZoneStrings, it returns an array of

•zoneStrings[i][0] - time zone ID
•zoneStrings[i][1] - long name of zone in standard time
•zoneStrings[i][2] - short name of zone in standard time
•zoneStrings[i][3] - long name of zone in daylight saving time

we can use 'time zone ID' in TimeZone.getTimeZone(), the rest as SimpleDateFormat 'z' General Time Zone

2) CDT is OK for SimpleDateFormat, try

new SimpleDateFormat("z").parse("CDT");

it works

1
votes

tl;dr

where can I find an open-source code for converting time strings into "Date" objects

Built into Java in the java.time classes.

LocalDate.parse( "2017-01-23" )
LocalTime.parse( "14:56:00" )
LocalDateTime.parse( "2017-01-23T07:56:00" )
OffsetDateTime.parse( "2017-01-23T07:56:00-07:00" )
ZonedDateTime.parse( "2017-01-23T07:56:00-07:00[America/Los_Angeles]" )

Using java.time

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

ZoneId

The java.util.TimeZone class is replaced by java.time.ZoneId and java.time.ZoneOffset. A time zone is a history of changes to a particular region’s offset-from-UTC.

There are many poor design decisions in the legacy java.time classes. Using the 3-4 character pseudo time zone codes is one such poor choice. Never use the 3-4 letter abbreviation such as CDT or EST or IST as they are not true time zones, not standardized, and not even unique(!).

Instead, you should specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland.

ISO 8601

Avoid using badly designed awkward formats such as "10:16:12.523 CDT Thu Mar 21 2013" which are hard to read by humans, difficult to parse by machine, rely on English needlessly, and make use of ambiguous (worse than useless!) pseudo time zones. Instead use the standard ISO 8601 formats. The java.time classes use these formats by default. The ZonedDateTime class extends the standard format by appending the continent/region time zone name in square brackets.

Examples:

  • 2017-01-23
  • 14:56:00
  • 2017-01-23T14:56:00Z
  • 2017-01-23T07:56:00-07:00[America/Los_Angeles]

For formats other than ISO 8601 use the DateTimeFormatter class. Already covered many hundreds of times already, so search Stack Overflow for more info and examples.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

0
votes

I would try to answer the 2nd part of your question. Try Joda-time

http://joda-time.sourceforge.net/index.html

Do let us know if it helps.