How can I convert multiple iso date format with the same function to date in java?

Ghiloufi :

I am creating a DSL with ANTLR 4, and I wonder if it is possible to convert several date format (string) to date with the same function without passing the format for example if my DSL is like that

date1 = "2020-05-08"
date2 = "2020/05/08"
date3 = "20200508"
...

and in my java code I convert the string directly to date without knowing the format for example

Date date1 = convertToDate(date1);
Date date2 = convertToDate(date2);
Date date1 = convertToDate(date3);

instead of writing

Date date1 = convertToDate(date1,"yyyy-mm-dd");
Date date2 = convertToDate(date2,"yyyy/mm/dd");
Date date1 = convertToDate(date3,"yyyymmdd");
cassiomolin :

Since Java 8, which introduced the new Date and Time API, you are no longer advised to use Date, Calendar, SimpleDateFormat and so on. Those are now legacy types.

Also, if you intend to parse the month, you are supposed to use MM instead of mm (which indicates minutes).

With the new Date and Time API, you could use DateTimeFormatter with optional patterns using [ and ]. Then you can parse the string dates to LocalDate instances, as shown below:

DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
        .appendPattern("[yyyy-MM-dd]")
        .appendPattern("[yyyy/MM/dd]")
        .appendPattern("[yyyyMMdd]")
        .toFormatter();

LocalDate date1 = LocalDate.parse("2020-05-08", dateFormatter);
LocalDate date2 = LocalDate.parse("2020/05/08", dateFormatter);
LocalDate date3 = LocalDate.parse("20200508", dateFormatter);

System.out.println(date1);
System.out.println(date2);
System.out.println(date3);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to Convert Date into ISO Date Format in javascript

How can I convert a date format to another date format in Swift?

How to convert ISO Date format to yyyymmddhsi format?

How can I convert Twitter API post time/date to valid ISO format?

How can I convert a date format in SQL?

how to convert UTC date-time into ISO 8601 format in Java?

How to convert String date into ISO Date format date

How can I convert "/Date(1399739515000)/" into date format in JavaScript?

How to convert a date with timezone to Javascript ISO format?

How can I parse a date (provided as a string) in the "ddMM" format and convert it to a Date Object in Java?

How can I format date by locale in Java?

How can I change the date format in Java?

how can I format a date in java with "ff"?

How can I convert this date-format in a format accepted by lubridate?

How to convert an ISO 8601 date to '/Date(1525687010053)/' format in javascript?

How do I convert String date to Date format date in dd-MM-yyyy format in java

How to convert the date format in java

Is there a Delphi RTL function that can convert the ISO 8601 basic date format to a TDate?

Convert string date to ISO format date

How can I convert a date into another format with moment.js?

How can I convert get date to format of yyyymmdd?

How can I convert the Date format for column in sqlite table?

How can I convert a custom `dayHour` string variable to date format?

How can I convert this query result to date format (SQL Server)

JavaScript How Can I convert this innerText to a valid date format?

How can I convert a date in YYYYMMDDHHMMSS format to epoch or unix time?

How can I convert a specific string format to date or datetime in Spark?

How to print the current time and date in ISO date format in java?

How can I format an ISO 8601 date to a more readable format, using Javascript?