Java gets all the days in a certain period ... __java learning

Source: Internet
Author: User
Original Address:Java gets all the dates in a certain period of time Author:Feier

Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Calendar;
Import Java.util.Date;
Import java.util.List;

public class Finddates
{

public static void Main (string[] args) throws Exception
{

Calendar cal = Calendar.getinstance ();
String start = "2012-02-01";
String end = "2012-03-02";
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Date Dbegin = Sdf.parse (start);
Date dend = Sdf.parse (end);
list<date> ldate = finddates (Dbegin, dend);
for (Date date:ldate)
{
System.out.println (Sdf.format (date));
}
}

 public static list<date> finddates (date dbegin, date dend)
 {
  list ldate = new ARR Aylist ();
  ldate.add (Dbegin);
  calendar calbegin = Calendar.getinstance ();
  //Sets the time for this Calendar with the given date
  calbegin.settime (dbegin);
  calendar Calend = Calendar.getinstance ();
  //Sets the time for this Calendar using the given date
  calend.settime (dend);
  //test whether this date is after the specified date
  while (Dend.after (Calbegin.gettime ()))
  {
   //According to the rules of the calendar, Adds or subtracts the specified amount of time for the given calendar field
   calbegin.add (Calendar.day_of_month, 1);
   ldate.add (Calbegin.gettime ());
  }
  return ldate;
 }
}

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.