xxxxxxxxxx
import datetime
# Example 1: Convert current date to string
current_date = datetime.datetime.now()
date_string = current_date.strftime("%Y-%m-%d") # Format as year-month-day
print(date_string)
# Example 2: Convert a specific date to string
specific_date = datetime.datetime(2022, 12, 31)
date_string = specific_date.strftime("%d/%m/%Y") # Format as day/month/year
print(date_string)
xxxxxxxxxx
const currentDate = new Date();
const dateString = currentDate.toLocaleString();
console.log(dateString); // Output: 2/15/2023, 10:30:15 AM (format may vary based on the user's locale)
xxxxxxxxxx
Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = formatter.format(date);
xxxxxxxxxx
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = new Date();
String dateTime = dateFormat.format(date);
System.out.println("Current Date Time : " + dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
xxxxxxxxxx
//Commons-lang DateFormatUtils
//Formats a date/time into a specific pattern
DateFormatUtils.format(yourDate, "yyyy-MM-dd HH:mm:SS");