When using Jparepository (a data access feature provided by spring data), you may encounter methods that query the corresponding entity classes by date.
Like what:
The entity class is: HappyBirthday
The field "Happy_date" in MySQL, the type is date (for example, 2016-12-22).
By implementing the Jparepository method:
Findbyhappydate (Date happydate);
To query for entities that have parameters equal to happydate.
When entering parameters, the time portion must be all 0, as in the following two ways:
1. Use Date:
Date temp = new Date ();
Temp.setdate (a);
Date happydate = null;
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");
try {
happydate = Df.parse (Df.format (date)),
} catch (ParseException e) {
e.printstacktrace ();
}
2, the first method is a bit gancient, you can use the second method, as follows:
Calendar cal = Calendar.getinstance ();
Cal.set (Calendar. Day_of_month, ();
Cal.set (Calendar. Hour_of_day, 0);
Cal.set (calendar.minute, 0);
Cal.set (Calendar.second, 0);
Cal.set (Calendar.millisecond, 0); A critical step, many times ignoring the millisecond 0, and can not query out the desired results
Date happydate = C.gettime ();
Next, query the database using the implemented method:
The first blog in life, so intersting!!!