java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
To convert the format of a java.util.Date
object from yyyy-MM-dd
to MM-dd-yyyy
, you can use the SimpleDateFormat
class and its format()
method.
Here's an example of how to do this:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat outputFormat = new SimpleDateFormat("MM-dd-yyyy");
Date date = inputFormat.parse("2022-01-01");
String output = outputFormat.format(date);
System.out.println(output); // Outputs: "01-01-2022"
}
}
In this example, the SimpleDateFormat
class is used to parse the input string "2022-01-01" into a Date
object using the yyyy-MM-dd
format. The format()
method is then used to format the Date
object into a string using the MM-dd-yyyy
format.
Note that the SimpleDateFormat
class is not thread-safe, so you should create a new instance of the class for each date that you want to format.
I hope this helps! Let me know if you have any questions.