Java obtains the query time between the first seven days and the last several days based on the current day.

Source: Internet
Author: User

Java obtains the query time between the first seven days and the last several days based on the current day.

Java obtains the query time between the first seven days and the last several days based on the current day.

Package com. kugou. schedu. service; import java. text. parseException; import java. text. simpleDateFormat; import java. util. calendar; import java. util. date; import java. util. gregorianCalendar; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. component; import com. kugou. schedu. dao. songHotDao; @ Componentpublic class SongHotService {public static void main (String [] args) throws ParseException {getDayBetween (7, 7 );} /*** obtain the start time and end time based on the current time ** @ param day * @ return start time and end time */public static Date [] getDayBetween (Integer day) {// Today's full-day Date startDt = new Date (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); String s = sdf. format (startDt. getTime (); try {startDt = sdf. parse (s);} catch (ParseException e) {e. printStackTrace ();} Calendar calendar = new GregorianCalendar (); calendar. setTime (new Date (startDt. getTime ()-1); calendar. add (calendar. DATE, day); // Long endDt = 604800L + startDt. getTime (); System. out. println (startDt); System. out. println (calendar. getTime (); return new Date [] {startDt, calendar. getTime ()};} /*** obtain the start time and end time based on the current time ** @ param day * obtain the time between several days ** @ param otherDay * starting from a few days ** @ return start time and end Time */public static Date [] getDayBetween (Integer day, integer otherDay) {// Today's full-day Date startDt = new Date (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); String s = sdf. format (startDt. getTime (); try {startDt = sdf. parse (s);} catch (ParseException e) {e. printStackTrace ();} Calendar calendarStart = new GregorianCalendar (); calendarStart. setTime (startDt); calendarStart. add (calendarStart. DATE,-7); startDt = calendarStart. getTime (); Calendar calendar = new GregorianCalendar (); calendar. setTime (new Date (startDt. getTime ()-1); calendar. add (calendar. DATE, 7); System. out. println (startDt); System. out. println (calendar. getTime (); return new Date [] {startDt, calendar. getTime () }}/ *** output: Fri Apr 17 00:00:00 CST 2015 Thu Apr 23 23:59:59 CST 2015 */


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.