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 */