In this code snippet, to achieve the "yyyy-MM-dd" format of a Calendar date, while keeping the Date object intact for Hibernate purpose comparisons, the following code can be implemented:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateConversionExample {
public static void main(String[] args) {
// Get the current date and add a day to it
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
Date date = cal.getTime();
// Change the date to yyyy-MM-dd format
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = format1.format(date); // This gives “yyyy-MM-dd” formatted string
// Print the returned date string
System.out.println("Formatted date: " + formattedDate);
// Assume we will pass this date object to hibernate
// No converting back to date, just use 'date'
}
}
Key Points: