Calculate the number of days and weeks between two dates using Calendar in Java

  • 2020-05-26 08:20:08
  • OfStack

preface

What exactly is an Calendar? The Chinese translation is calendar, then we can immediately think of our life there are Yang (public) calendar, Yin (agricultural) calendar. What's the difference?

Such as:

The definition of month - Yang '(public) calendar year 12 months, the days of each month are different; Yin (agriculture) calendar, each month fixed 28 days

The first day of the week - Sunday is the first day of the week. In the Yin (farming) calendar, Monday is the first day

In fact, there have been many epochs throughout history. The difference between them is too big. For example, if a person's birthday is "August 8", then one could be August 8 in a Yang (public) calendar, but it could also be a Yin (agricultural) calendar. So for the timing system 1, you must specify the choice of a calendar. Now the most popular and universal calendar is "Gregorian Calendar". That is, when we talk about years, we often use "the years of the Christian era." The Calendar abstract class defines enough methods for us to express the rules of the calendar. Java itself provides an implementation of the "Gregorian Calendar" rule. We learn from Calendar.getInstance() The instance you get is an "GreogrianCalendar" object (passed to you) new GregorianCalendar() Results obtained 1 to). Without further ado, take a look at the body of this article.

An 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 the 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; 
 } 

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){ 
    // discontent 1 Week of press 1 Weeks to calculate  
    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 return null 
    return null; 
   } 
  } 
  return null; 
 } 

conclusion

The above is the whole content of calculating days and weeks between two dates by using Calendar in Java. I hope the content of this paper can bring you some help in your study or work. If you have any questions, you can leave a message to communicate.


Related articles: