Packagecom.dada.test; ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;/** * @author * @version * */ Public classDateutil {/*** Default date format*/ Public StaticString Default_format = "Yyyy-mm-dd"; /*** Test Main method *@paramargs*/ Public Static voidMain (string[] args) { for(inti = 1951;i < 1960;i++) {System.out.println (FormatDate (Getyearfirst (i))); System.out.println (FormatDate (Getyearlast (i))); } System.out.println (FormatDate (Getcurryearfirst ())); System.out.println (FormatDate (Getcurryearlast ())); } /*** Formatted date *@paramDate Day Object *@returnString Date String*/ Public StaticString formatdate (date date) {SimpleDateFormat F=NewSimpleDateFormat (Default_format); String sdate=F.format (date); returnsdate; } /*** Get the first day of the year *@paramYear *@return */ Public StaticDate Getcurryearfirst () {Calendar currcal=calendar.getinstance (); intCurrentYear =Currcal.get (calendar.year); returnGetyearfirst (currentyear); } /*** Get the last day of the year *@paramYear *@return */ Public StaticDate Getcurryearlast () {Calendar currcal=calendar.getinstance (); intCurrentYear =Currcal.get (calendar.year); returnGetyearlast (currentyear); } /*** Get the first day of the year date *@paramYear years *@returnDate*/ Public StaticDate Getyearfirst (intYear ) {Calendar Calendar=calendar.getinstance (); Calendar.clear (); Calendar.set (Calendar.year, year); Date Curryearfirst=Calendar.gettime (); returnCurryearfirst; } /*** Get the date of the last day of the year *@paramYear years *@returnDate*/ Public StaticDate Getyearlast (intYear ) {Calendar Calendar=calendar.getinstance (); Calendar.clear (); Calendar.set (Calendar.year, year); Calendar.roll (Calendar.day_of_year,-1); Date Curryearlast=Calendar.gettime (); returnCurryearlast; } }
Java gets the first day and last day of the year