Home > Java > javaTutorial > 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?

DDD
Release: 2024-11-04 09:03:30
Original
862 people have browsed it

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

<code class="java">LocalDateTime ldt = LocalDateTime.now();
System.out.println(DateTimeFormatter.ofPattern("MM-dd-yyyy", Locale.ENGLISH).format(ldt));</code>
Copy after login

Java 7- Approach

<code class="java">// Import ThreeTen Backport
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter f = DateTimeFormatter.ofPattern("MM-dd-yyyy");
String formattedDateTime = f.format(ldt);</code>
Copy after login

Original Solution

<code class="java">Date myDate = new Date();
SimpleDateFormat mdyFormat = new SimpleDateFormat("MM-dd-yyyy");
String mdy = mdyFormat.format(myDate);</code>
Copy after login

Note: Formatting does not alter the Date value itself.

The above is the detailed content of How to Convert a java.util.Date from yyyy-mm-dd to mm-dd-yyyy?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template