10.How to convert Date to String.
💡Code:
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringConversion7078ash {
public static void main(String[] args) {
// Example Date
Date currentDate = new Date();
// Convert the Date to a String
String convertedString = convertDateToString(currentDate);
// Print the converted String
System.out.println("Converted String: " + convertedString);
}
// Function to convert a Date to a String
private static String convertDateToString(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Use the format method to convert the Date to a String
return dateFormat.format(date);
}
}
📸Output :