ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;ImportJava.util.Calendar;Importjava.util.List;/*** Time Range Tool Class * *@authorZwq **/ Public classTimeframeutil {/*** Get today's time range *@returnreturns a collection of strings of length 2, such as: [2017-11-03 00:00:00, 2017-11-03 24:00:00]*/ Public StaticList<string>Gettoday () {List<String> dataList =NewArraylist<> (2); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); Calendar Calendar=calendar.getinstance (); Calendar.settimeinmillis (System.currenttimemillis ()); Calendar.add (Calendar.date,0); String Today=Dateformat.format (Calendar.gettime ()); Datalist.add (Today+ "00:00:00"); Datalist.add (Today+ "24:00:00"); returndataList; } /*** Get yesterday's time range *@returnreturns a collection of strings of length 2, such as: [2017-11-02 00:00:00, 2017-11-02 24:00:00]*/ Public StaticList<string>Getyesterday () {List<String> dataList =NewArraylist<> (2); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); Calendar Calendar=calendar.getinstance (); Calendar.settimeinmillis (System.currenttimemillis ()); Calendar.add (Calendar.date,-1); String Yesterday=Dateformat.format (Calendar.gettime ()); Datalist.add (Yesterday+ "00:00:00"); Datalist.add (Yesterday+ "24:00:00"); returndataList; } /*** Get the time range of the week *@returnreturns a collection of strings of length 2, such as: [2017-10-30 00:00:00, 2017-11-05 24:00:00]*/ Public StaticList<string>Getcurrentweek () {List<String> dataList =NewArraylist<> (2); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); Calendar Calendar=calendar.getinstance (); Calendar.setfirstdayofweek (calendar.monday);//set Monday as the first day of the weekCalendar.settimeinmillis (System.currenttimemillis ()); Calendar.set (Calendar.day_of_week, calendar.monday); String Monday= Dateformat.format (Calendar.gettime ()) + "00:00:00"; Calendar.set (Calendar.day_of_week, calendar.sunday); String Sunday= Dateformat.format (Calendar.gettime ()) + "24:00:00"; Datalist.add (Monday); Datalist.add (Sunday); returndataList; } /*** Get the time range for this month *@returnreturns a collection of strings of length 2, such as: [2017-11-01 00:00:00, 2017-11-30 24:00:00]*/ Public StaticList<string>Getcurrentmonth () {List<String> dataList =NewArraylist<> (2); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); Calendar Calendar=calendar.getinstance (); Calendar.settimeinmillis (System.currenttimemillis ()); Calendar.add (Calendar.month,0); Calendar.set (Calendar.day_of_month,1); String Firstdayofmonth= Dateformat.format (Calendar.gettime ()) + "00:00:00"; Calendar.add (Calendar.month,1); Calendar.set (Calendar.day_of_month,0); String Lastdayofmonth= Dateformat.format (Calendar.gettime ()) + "24:00:00"; Datalist.add (Firstdayofmonth); Datalist.add (Lastdayofmonth); returndataList; } /*** Get the time range of the year *@returnreturns a collection of strings of length 2, such as: [2017-01-01 00:00:00, 2017-12-31 24:00:00]*/ Public StaticList<string>getcurrentyear () {List<String> dataList =NewArraylist<> (2); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); Calendar Calendar=calendar.getinstance (); Calendar.settimeinmillis (System.currenttimemillis ()); Calendar.add (Calendar.year,0); Calendar.set (Calendar.day_of_year,1); String Firstdayofyear= Dateformat.format (Calendar.gettime ()) + "00:00:00"; Calendar.add (Calendar.year,1); Calendar.set (Calendar.day_of_year,0); String Lastdayofyear= Dateformat.format (Calendar.gettime ()) + "24:00:00"; Datalist.add (firstdayofyear); Datalist.add (lastdayofyear); returndataList; }}
Java Gets the tool class for today, yesterday, this week, this month, and this year's time range