Java gets the first day and the last day of the year

Source: Internet
Author: User

 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

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.