"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 dd/MM/yyyy to yyyy/MM/dd Date Format in Java?

How to Convert dd/MM/yyyy to yyyy/MM/dd Date Format in Java?

Posted on 2025-03-23
Browse:390

How to Convert dd/MM/yyyy to yyyy/MM/dd Date Format in Java?

Changing Date Format in Java: Transforming dd/MM/yyyy to yyyy/MM/dd

To alter the date format from dd/MM/yyyy to yyyy/MM/dd in Java, you can utilize the SimpleDateFormat class. Here's how to proceed:

  1. Define the original and new date formats as String constants:
final String OLD_FORMAT = "dd/MM/yyyy";
final String NEW_FORMAT = "yyyy/MM/dd";
  1. Consider the original date string, for instance:
String oldDateString = "12/08/2010"; // Assuming August 12, 2010
  1. Create a SimpleDateFormat object using the original format:
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
  1. Parse the original date string using this SimpleDateFormat object into a Date object:
Date d = sdf.parse(oldDateString);
  1. Subsequently, adapt the SimpleDateFormat object to use the new format:
sdf.applyPattern(NEW_FORMAT);
  1. Finally, format the Date object using the updated SimpleDateFormat object to obtain the new date string:
String newDateString = sdf.format(d);

This approach allows you to successfully convert the date string from the original format (dd/MM/yyyy) to the desired format (yyyy/MM/dd).

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