Calculates the number of days and weeks between two dates using the calendar in Java _java

Source: Internet
Author: User

Objective

What exactly is a Calendar? Chinese translation is the calendar, then we can immediately think of our lives in the Yang (public) calendar, Yin (Nong) calendar points. What is the difference between them?

For example, there are:

Definition of month-yang ' (gong) calendar 12 months a year, the number of days each month is different; Yin (Nong) calendar, fixed 28 days per month

The first day of the week-yang (male) calendar Sunday is the first day; Yin (Nong) calendar, Monday is the first day

In fact, there are many eras in the history of the method. Their differences are too big, such as a person's birthday is "August 8" so one may be Yang (public) Calendar of August 8, but can also be Yin (agricultural) calendar date. So in order to timing the unification, you must specify a calendar choice. Now the most popular and universal calendar is "Gregorian calendar." That is, we often use "year A.D." in the story. The calendar abstract class defines enough methods to allow us to express the rules of the calendar. Java itself provides an implementation of the "Gregorian Calendar" rule. The example we get from this Calendar.getInstance() is a "Greogriancalendar" object ( new GregorianCalendar() consistent with the results you get). The following words do not say more, to see the text of this article.

The algorithm that uses the Calendar class in Java to calculate the number of days and weeks between two dates!

Calculate the number of days between dates:

public Object Counttwodate (object StartDate, Object EndDate) 
 { 
  if (Stringutils.isnotempty (startdate) & & Stringutils.isnotempty (EndDate)) 
  { 
   date start= (date) StartDate; 
   Date end = (date) enddate; 
   Calendar cal=calendar.getinstance (); 
   Cal.settime (start); 
   Long Time1=cal.gettimeinmillis (); 
   Cal.settime (end); 
   Long Time2=cal.gettimeinmillis (); 
   Long between_days= (time2-time1)/(1000*3600*24); 
   Return Integer.parseint (string.valueof (between_days)); 
  } 
  return null; 
 

And then calculate the number of weeks:

public Object Counttwodayweek (object StartDate, Object EndDate) 
 { 
  if (Stringutils.isnotempty (startdate) & & Stringutils.isnotempty (EndDate)) 
  { 
   date start= (date) StartDate; 
   Date end = (date) enddate; 
   Calendar cal=calendar.getinstance (); 
   Cal.settime (start); 
   Long Time1=cal.gettimeinmillis (); 
   Cal.settime (end); 
   Long Time2=cal.gettimeinmillis (); 
   Long between_days= (time2-time1)/(1000*3600*24); 
   Double days=double.parsedouble (string.valueof (between_days)); 
   if ((DAYS/7) >0 && (DAYS/7) <=1) {//Less than one week in a week return 
    1; 
   } else if (days/7>1) { 
    int day=days.intvalue (); 
    if (day%7>0) {return 
     day/7+1; 
    } else{return 
     DAY/7 
    } 
   } else if ((DAYS/7) ==0) {return 
    0; 
   } else{ 
    //negative returns NULL return 
    null; 
   } 
  return null; 
 

Summarize

The above is to use the calendar in Java to calculate the number of days between two dates and weeks of all the content, I hope this article on the content of everyone's study or work can bring some help, if you have questions you can message exchange.

Related Article

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.