0

I want to convert pre Java8 like below.

DateFormat formatter = new SimpleDateFormat(timestampPattern, locale);
Date dt = formatter.parse(timestamp);
Date currentDateTime = getCurrentTime();

To Java 8 code to support more than 3 digits in milliseconds. I found ways to do that using the following simple code.

String parsedate="2016-03-16 01:14:21.6739";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSS"); 
LocalDateTime newdate = LocalDateTime.parse(parsedate, dateTimeFormatter);

Problem with above code is that you have to know specific class before parsing it like if it contains only date in you have to use LocalDate and if only time LocalTime and if date + time + zone you have to use ZonedDateTime.

My problem is that I don't know timestampPattern or timestamp(Given in pre Java8 code snippet) before hand(as it is user input) hence can't choose subclass in my code. Is there any better way for this?

Naman
  • 27,789
  • 26
  • 218
  • 353
AAjit
  • 61
  • 1
  • 10
  • Your title does not match your Question. Your Question is about formatting patterns, but your title is about the various date-time types. Please correct this to keep Stack Overflow tidy. – Basil Bourque Nov 23 '18 at 17:01
  • Have an array of string containing all the patterns. Write a custom parser. In the parser iterate through the array and try to parse. If it is not of that format, you will get ParseException. Catch it and continue through the loop. You can refer [this link](https://stackoverflow.com/questions/7579227/how-to-get-the-given-date-string-formatpattern-in-java). – tejasrbhat Nov 23 '18 at 08:55

1 Answers1

1

The following code will show you how you could deal with it:

String parsedate="2016-03-16 01:14:21.6739";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSS");
TemporalAccessor parsed = dateTimeFormatter.parse(parsedate);
System.out.println(parsed.getClass());
// with the Parsed object you can then construct what you require... e.g. LocalDate:
System.out.println(LocalDate.from(parsed));
// or LocalDateTime:
System.out.println(LocalDateTime.from(parsed));

This prints:

class java.time.format.Parsed
2016-03-16
2016-03-16T01:14:21.673900

So you just have to use what you require in your code and build your LocalDate or LocalDateTime from the Parsed-object.

Note, that if the user can input only yyyy-MM-dd and you would have used such a date time format, then you will get problems creating the LocalDateTime from it, but I think you usually know which target type you require. Otherwise you could even just have worked with the TemporalAccessor instead.

In order to solve the specific date type issue you may need either to work against exceptions (try to parse it (or call from from the Parsed-object) and fallback to the next possible date format or type) or just check the format beforehand and use the appropriate date formatter and type then, which I rather recommend.

Roland
  • 22,259
  • 4
  • 57
  • 84