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