"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Convert a java.util.Date from yyyy-mm-dd to mm-dd-yyyy?

How to Convert a java.util.Date from yyyy-mm-dd to mm-dd-yyyy?

Published on 2024-11-08
Browse:578

How to Convert a java.util.Date from yyyy-mm-dd to mm-dd-yyyy?

Conversion of java.util.Date Format from yyyy-mm-dd to mm-dd-yyyy

Understanding Dates

java.util.Date stores the milliseconds since the Unix epoch. It is not formatted, hence formatting has no impact on the underlying Date value.

Java 8 Approach

LocalDateTime ldt = LocalDateTime.now();
System.out.println(DateTimeFormatter.ofPattern("MM-dd-yyyy", Locale.ENGLISH).format(ldt));

Java 7- Approach

// Import ThreeTen Backport
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter f = DateTimeFormatter.ofPattern("MM-dd-yyyy");
String formattedDateTime = f.format(ldt);

Original Solution

Date myDate = new Date();
SimpleDateFormat mdyFormat = new SimpleDateFormat("MM-dd-yyyy");
String mdy = mdyFormat.format(myDate);

Note: Formatting does not alter the Date value itself.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3