2

In my csv file I have column contains date like this :

my_col,...
2016-06-28 21:05:56 ADT
2016-06-28 22:05:56 ADT
2016-06-28 23:05:56 ADT

I have to map this CSV file to a Java object.

I tried multiple solution like :

final String str = "2016-06-28 14:18:28 ADT";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss");
LocalDateTime ld2 = LocalDateTime.parse( str , f ) ;

But I have error :

java.time.format.DateTimeParseException: Text '2016-06-28 14:18:28 ADT' could not be parsed, unparsed text found at index 19

How can I convert this kind of date (2016-06-28 14:18:28 ADT) to my Java field?

And which API shows me how to map a CSV file to a list of Java objects?

8
  • Your data needs to match your pattern, and you've got an "ADT" in there you're not accounting for. Commented Oct 11, 2019 at 21:15
  • i dont know what is the ADT in my dates. i have to read the csv file as it is an theses dates must be stored in my java object, i dont know realy how to do after several tests, Commented Oct 11, 2019 at 21:17
  • 1
    It's the timezone Commented Oct 11, 2019 at 21:17
  • 1
    i tried this : final String dateTime1 = "2016-06-29 00:38:03 ADT"; DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT; final ZonedDateTime parsed = ZonedDateTime.parse(dateTime1, formatter.withZone(ZoneId.of("Canada/Atlantic")));--final String str = "2016-06-29 00:38:03 ADT"; DateTimeFormatter f = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss"); LocalDate ld = LocalDate.parse( str , DateTimeFormatter.ISO_LOCAL_DATE ) ; LocalDate ld = LocalDate.parse( str , f ) ; no one work Commented Oct 11, 2019 at 21:27
  • 1
    I suggest you educate the publisher of your CSV data about the practice of communicating moments by using UTC (an offset of zero) and ISO 8601. In Java, see the Instant class. Commented Oct 12, 2019 at 4:09

1 Answer 1

1

For reading csv in java you could use libraries like e.g. this one.

As for the question in which attribute/object the parsed date should end up

"so my java object field must be of type : ZonedDateTime ?" No, you could use a string, split it in several Integers, use DateTime plus a String for the time zone ... as you like it.

As for your pattern, check out the oracle documentation on datetimeformatter patterns, you can use this:

DateTimeFormatter f = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss z");
System.out.println(ZonedDateTime.parse("2016-06-28 14:18:28 ADT", f));
    says "2016-06-28T14:18:28-03:00[SystemV/AST4ADT]"

where "SystemV/AST4ADT" on one side shows up in respectable lists but on the other side seems to be an outdated convention (from SystemV).

Now in comments it has been noted that the same input string on other systems yields

2016-06-28T14:18:28-03:00[America/Halifax]

and I think, that can be traced back to the setup of your local system, more precisely, the jvm time zone settings and the local time zone of your machine (see below).

Considering the additional info from the comments it becomes clear, that you can't realiably recover "ADT" (literally) from your input string, taking the route over ZonedDateTime. If you want to retain it, something like "2016-06-28 14:18:28 ADT".substring(21) might be the most economical version, ugly as it is. I'd take it.

There are alternatives, though. Chances are, all your date values stem from the same time zone anyway and you don't need it in the first place. If you do, you could create a mapping between all possible time zones, 425 at the moment, to their abbreviations (zonedDateTime.getZone()->String).

Now it's getting really esoteric, there appears to be a mysterious time zone updater tool by Oracle, which is supposed to keep JVM's time zones up to date and which takes it's values from a time zone database, the currently maintained version is IANA's Time Zone Database, when downloaded it contains an theory.html with a section "Time zone abbreviations" which says: "When this package is installed, it generates time zone abbreviations like 'EST' to be compatible with human tradition and POSIX". Then I discovered to my delight that on linux there's the zdump command which responds to zdump Canada/Atlantic UTC with

Canada/Atlantic Sat Oct 12 03:39:19 2019 ADT

You can extract the whole list, I put the command and the output in a github gist. What you don't get is a decoding for e.g "SystemV/AST4ADT". The whole SystemV nomenclature seems to be deprecated, so I wonder, how this pops up in ZonedDateTime. Where I eventually found it was the "systemv" file in ican's database, you can find the relevant content below.

There's also a web service provider out there where you can find a hopefully complete list of time zones plus abbreviations and you could probably use the service, if you're into this kind of thing. Doesn't account for stuff like "SystemV/AST4ADT", though.

Content of "systemv" file, you'd have to map to the Time Zone Database abbreviations using the GMT-offsets (like -4:00)

tzdb data for System V rules (this file is obsolete)
Zone    NAME        STDOFF  RULES/SAVE  FORMAT  [UNTIL]
Zone    SystemV/AST4ADT -4:00   SystemV     A%sT
Zone    SystemV/EST5EDT -5:00   SystemV     E%sT
Zone    SystemV/CST6CDT -6:00   SystemV     C%sT
Zone    SystemV/MST7MDT -7:00   SystemV     M%sT
Zone    SystemV/PST8PDT -8:00   SystemV     P%sT
Zone    SystemV/YST9YDT -9:00   SystemV     Y%sT
Zone    SystemV/AST4    -4:00   -       AST
Zone    SystemV/EST5    -5:00   -       EST
Zone    SystemV/CST6    -6:00   -       CST
Zone    SystemV/MST7    -7:00   -       MST
Zone    SystemV/PST8    -8:00   -       PST
Zone    SystemV/YST9    -9:00   -       YST
Zone    SystemV/HST10   -10:00  -       HST

Sign up to request clarification or add additional context in comments.

13 Comments

thank you, i tried this : final String str = "2016-06-29 00:38:03 ADT"; DateTimeFormatter f = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss"); LocalDate ld = LocalDate.parse( str , DateTimeFormatter.ISO_LOCAL_DATE ) ; LocalDate ld = LocalDate.parse( str , f ) ; i obtainer : 2016-06-28T14:18:28 but if i want re writ since my java field to csv file how can i obtain again the : "2016-06-28 14:18:28 ADT" ?
why don't you go with the pattern above? It's tested for your case. Note the "z" at the end. "yyyy-MM-dd HH:mm:ss z" If you're asking, how to preserve the "ADT" with LocalDate, you can't. From the oracle documentation: "A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03. "
yes I tried this code : final String str = "2016-06-28 14:18:28 ADT"; DateTimeFormatter f = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss z"); LocalDateTime ld2 = LocalDateTime.parse( str , f ) ; System.out.println(ld2); i obtained this : 2016-06-28T14:18:28. but i should re write from the java filed to csv file so from : "2016-06-28T14:18:28" i should obtain again "2016-06-28 14:18:28 ADT" to re write it as it is in my csv file how can i do that please?
thank you, i tried this : final String str = "2016-06-29 00:38:03 ADT"; DateTimeFormatter f = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss"); LocalDate ld = LocalDate.parse( str , DateTimeFormatter.ISO_LOCAL_DATE ) ; LocalDate ld = LocalDate.parse( str , f ) ; i obtainer : 2016-06-28T14:18:28 but if i want re writ since my java field to csv file how can i obtain again the : "2016-06-28 14:18:28 ADT" ?
there is no maner to done it for exemple by ZoneId zone = ZoneId.of("Canada/Atlantic"); instead of concat manualy "ADT" ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.