in Education by
We have a library where users can pass in dates in multiple formats. They follow the ISO but are abbreviated at times. So we get things like "19-3-12" and "2019-03-12T13:12:45.1234" where the fractional seconds can be 1 - 7 digits long. It's a very large number of combinations. DateTimeFormatter.parseBest doesn't work because it won't accept "yy-m-d" for a local date. The solutions here won't work because it assumes we know the pattern - we don't. And telling people to get their string formats "correct" won't work as there's a ton of existing data (these are mostly in XML & JSON files). My question is, how can I parse strings coming in in these various pattersn without have to try 15 different explicit patterns? Or even better, is there some way to parse a string and it will try everything possible and return a Temporal object if the string makes sense for any date[time]? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Without a full specification it is hard to give a precise recommendation. The techniques generally used for variable formats include: Trying a number of known formats in turn. Optional parts in the format pattern. DateTimeFormatterBuilder.parseDefaulting() for parts that may be absent from the parsed string. As you are aware, parseBest. I am assuming that y-M-d always come in this order (never M-d-y or d-M-y, for example). 19-3-12 conflicts with ISO 8601 since the standard requires (at least) 4 digit year and 2 digit month. A challenge with 2-digit year is guessing the century: is this 1919 or 2019 or might it be 2119? The good news: presence and absence of seconds and varying number of fractional digits are all built-in and pose no problem. From what you have told us it seems to me that the following is a fair shot. DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("[uuuu][uu]-M-d") .optionalStart() .appendLiteral('T') .append(DateTimeFormatter.ISO_LOCAL_TIME) .optionalEnd() .toFormatter(); TemporalAccessor dt = formatter.parseBest("19-3-12", LocalDateTime::from, LocalDate::from); System.out.println(dt.getClass()); System.out.println(dt); Output: class java.time.LocalDate 2019-03-12 I figure that it should work with the variations of formats that you describe. Let’s just try your other example: dt = formatter.parseBest( "2019-03-12T13:12:45.1234", LocalDateTime::from, LocalDate::from); System.out.println(dt.getClass()); System.out.println(dt); class java.time.LocalDateTime 2019-03-12T13:12:45.123400 To control the interpretation of 2-digit year you may use one of the overloaded variants of DateTimeFormatterBuilder.appendValueReduced(). I recommend that you consider a range check on top of it.

Related questions

0 votes
    In java, how can I parse json text to get value of variables like post_id ? Here is my json text { "page": { ... ": "1234567890" } ] } Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I have been wrestling with this for a while. I am trying to parse a JSON Api into a UITableview. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    Which design pattern suggests multiple classes through which request is passed and multiple but only relevant classes carry ... of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    I am trying to write a program that reads data from hundreds of YAML files repetitively and stores certain ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    If the weight matrix stores multiple associations among several patterns, then network becomes? (a) autoassoiative ... temporal associative memory Please answer the above question....
asked Aug 27, 2022 in Education by JackTerrance
0 votes
    Which of these method of Thread class is used to Suspend a thread for a period of time? (a) sleep() ... portion Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    What is true about time slicing? (a) Time slicing is OS service that allocates CPU time to available ... Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these method returns an instance of DateFormat that can format time information? (a) getTime() (b) ... Expressions of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of these class allows us to define our own formatting pattern for dates and time? (a) DefinedDateFormat ... Expressions of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of these formatting strings of SimpleDateFormat class is used to print AM or PM in time? (a) a (b ... Regular Expressions of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of this package is used for analyzing code during run-time? (a) java.applet (b) java.awt (c) ... Interfaces & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these class allows us to get real time data about private and protected member of a class? (a) java. ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following is a best practice to measure time taken by a process for execution? (a) System. ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following contains both date and time? (a) java.io.date (b) java.sql.date (c) java.util.date ... Beans & JDBC of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    I'm looking to update a site for some friends with a pretty simple request (to change the time on each slide ... and the .php from the template that populates the slideshow:...
asked May 14, 2022 in Education by JackTerrance
...