Home  >  Article  >  Java  >  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
DDDOriginal
2024-11-04 09:03:30844browse

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>

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>

Original Solution

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

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!

Statement:
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