I need to convert a Date in java:
Date date=calendar.getTime();
// yyyy-MM-dd
I want to turn it into a String "yyyy-MM-dd HH:mm:ss" so i can insert it into a table in MySQL. Any ideea?
I need to convert a Date in java:
Date date=calendar.getTime();
// yyyy-MM-dd
I want to turn it into a String "yyyy-MM-dd HH:mm:ss" so i can insert it into a table in MySQL. Any ideea?
2
Answers
To convert a Date object to a String in the format "yyyy-MM-dd HH:mm:ss", you can use the SimpleDateFormat class in Java.
I made an example for you, you first obtain the current date and time using the Calendar class. Then, you create an instance of SimpleDateFormat and specify the desired format pattern as "yyyy-MM-dd HH:mm:ss". Finally, you format the Date object using the format() method of SimpleDateFormat, which returns the formatted date string.
You can then use the dateString variable to insert the formatted date into a MySQL table
You can use SimpleDateFormat:
or