Jparepository about date (without time) query considerations

Source: Internet
Author: User


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!!!





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.