Java time Date Processing class instance _java

Source: Internet
Author: User

The example in this article describes the Java time Date processing class, which is used to traverse every day between two dates. Share to everyone for your reference. as follows:

/** * * * FileName: Accountdate.java * * * Create Time: 2008-11-18 * * Email: **@163.com/import Java.text.DecimalFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;
 public class Accountdate {private static transient int gregoriancutoveryear = 1582;
 /** leap year days per month * * private static final int[] Days_p_month_ly= {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 /** non-leap year days per month/private static final int[] Days_p_month_cy= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
 /** represents the year, month, and day of the array/private static final int Y = 0, M = 1, D = 2; /** * The string representing the date is divided into an array of integers representing the day and month * @param date * @return/public static int[] Splitymd (string date) {date = Date.repl
  Ace ("-", "");
  Int[] Ymd = {0, 0, 0};
  Ymd[y] = integer.parseint (date.substring (0, 4));
  Ymd[m] = Integer.parseint (date.substring (4, 6));
  Ymd[d] = Integer.parseint (date.substring (6, 8));
 return YMD; /** * Checks whether the incoming parameter represents a leap year * @paramYear * @return/public static Boolean isleapyear (int year) {return year >= gregoriancutoveryear? ((year%4 = 0) && ((year%100!= 0) | | (year%400 = 0)) 
 : (year%4 = 0); /** * Date plus 1 days * @param year * @param month * @param day * @return/private static int[] Addoneday (int year, int
   month, int day) {if (Isleapyear (year)) {day++;
    if (Day > Days_p_month_ly[month-1]) {month++;
     if (month >) {year++;
    month = 1;
   } day = 1;
   }}else{day++;
    if (Day > Days_p_month_cy[month-1]) {month++;
     if (month >) {year++;
    month = 1;
   } day = 1;
  } int[] Ymd = {year, month, day};
 return YMD; /** * The month or date less than two digits is replenished to two-bit * @param decimal * @return/public static String formatmonthday (int decimal) {Decimalfo
  Rmat df = new DecimalFormat ("00");
 return Df.format (decimal); /** * To make up less than four-bit years for four-bit * @param decimal * @return/public static String formatyear (int decimAL) {DecimalFormat df = new DecimalFormat ("0000");
 return Df.format (decimal); /** * Calculates the number of days between two dates * @param begin * @param end * @return * @throws parseexception/public static long COUNTDA
   Y (String begin,string end) {SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
   Date Begindate, EndDate;
   Long day = 0;
   try {begindate= format.parse (begin);
   Enddate= format.parse (end); 
  Day= (Enddate.gettime ()-begindate.gettime ())/(24*60*60*1000);
  catch (ParseException e) {e.printstacktrace ();
 Return day; /** * Calculates the date in a circular manner * @param begindate EndDate * @param days * @return/public static list<string> Geteveryd
  Ay (string begindate, String enddate) {Long days = Countday (Begindate, EndDate);
  int[] Ymd = Splitymd (begindate);
  list<string> everydays = new arraylist<string> ();
  Everydays.add (begindate);
   for (int i = 0; i < days; i++) {ymd = Addoneday (Ymd[y], ymd[m], ymd[d]); Everydays.add (FormAtyear (Ymd[y]) + "-" +formatmonthday (Ymd[m]) + "-" +formatmonthday (ymd[d));
 return everydays; public static void Main (string[] args) {list<string> List = Accountdate.geteveryday ("2008-08-29", "2008-09-02"
  );
  for (String result:list) {System.out.println (result); }
 }
}

I hope this article will help you with your Java programming.

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.