Java Date Processing

Source: Internet
Author: User

Daily work often encountered about the date of processing, the following to be written from a good Java code snippet to share, also as a backup, but also welcome to exchange, if you share please indicate the source, thank you.

1. Returns the month between two time periods:

  

/*** Returns all months between any two months, returning with list * For example, start and end month parameter is 2013-12,2014-02 return 2013-12,2014-01,2014-02 list *@authorHsuchan *@versionv1.0 2014-10-14 *@paramStartDate *@paramEndDate *@returnlist<string> *@throwsparseexception*/     Public StaticList<string> Getmonthbetween (String startdate,string endDate)throwsparseexception {ArrayList<String> ymlist =NewArraylist<string>(); SimpleDateFormat SDF=NewSimpleDateFormat ("yyyy-mm"); Date MinDate=Sdf.parse (StartDate); Date MaxDate=Sdf.parse (endDate); Calendar min=calendar.getinstance (); Calendar Max=calendar.getinstance ();        Min.settime (mindate); Min.set (Min.get (calendar.year), Min.get (calendar.month),1);        Max.settime (MaxDate); Max.set (Max.get (calendar.year), Max.get (calendar.month),2); Calendar Curr= min;//Temp Variable         while(Curr.before (max)) {//The loop is added to the list only before the maximum monthYmlist.add (Sdf.format (Curr.gettime ())); Curr.add (Calendar.month,1);//Add one sequentially        }        returnymlist; }

Java Date Processing

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.