Search code examples
javadate-format

DateFormat To DateTimeFormatter


I can get rid of this issue, and i don't found the answer rigth now.

Is it possible to convert DateFormat on DateTimeFormatter?

DateFormat dateFormat =  new SimpleDateFormat("MM/dd/yy HH:mm:ss", Locale.getDefault);

I want to make something like that :

DateTimeFormatter.ofPattern((String)dateFormatter);

Or

(DateTimeFormatter)dateFormatter;

Or is there a way to get the String that make the format in the DateFormat?

I want to do that beacause i've read that the DateTimeFormater is the Java8+ new way to deal with Date, and i need to keep the old one because it's used a lot of times elswhere in the Project.

Thanks!


Solution

  • You can use following code.

     DateTimeFormatter dateTimeFormatter = 
        DateTimeFormatter.ofPattern(((SimpleDateFormat) dateFormat).toPattern());