Reputation: 115
How to convert a DateTimeFormatter to DateFormat ? I want to cast DateTimeFormatter to DateFormat ? Is it possible ?
I have a object of DateTimeFormatter and I want to pass it to a method whose parameter only accepts DateFormat.
Upvotes: 7
Views: 6428
Reputation: 338181
There is no backward conversion from java.time.format.DateTimeFormatter
to the old formatter class.
The newer java.time classes are intended to supplant the old ones. They are not intended to be mixed or interoperate. The old date-time classes have proven to be poorly designed, confusing, and troublesome.
The old date-time classes bundled with the earliest versions of Java are now legacy. Classes such as java.util.Date/.Calendar and java.text.DateFormat/.SimpleDateFormat should be avoided.
Those old classes have been supplanted by the java.time framework built into Java 8 and later. Much of that functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project, and further adapted to Android in the ThreeTenABP project.
Instead, go forward. Convert your old date-time objects to the modern java.time classes. New methods have been added to the old classes to facilitate conversion of your data.
If you have a java.util.Date, call toInstant()
. If you have a java.util.Calendar object, convert to a ZonedDateTime
.
To get started, see my Question and my Answer with nifty diagram of types.
Upvotes: 2