Preface
What exactly is a Calendar? The Chinese translation is calendar, so we can immediately think of the difference between yang (public) and yin (agricultural) calendars in our lives. What is the difference between them?
For example:
Definition of month - 12 months in a year, the number of days in each month is different; yin (agricultural) calendar, fixed 28 days per month
The first day of the week - Sunny (public) calendar Sunday is the first day; Yin (agricultural) calendar, Monday is the first day
In fact, there are many epoch-like methods in history. Their differences are too big. For example, a person's birthday is "August 8". Then one may be the eighth day of the Yang (public) Gregorian calendar, but it can also be the date of the Yin (agricultural) calendar. Therefore, in order to unify the timing, a calendar selection must be specified. The most popular and common calendar now is "Gregorian Calendar". That is, when we talk about years, we often use "A.D.". 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 instance we get from Calendar.getInstance() is a "GreogrianCalendar" object (as consistent with the result you get with new GregorianCalendar() ). I won’t say much below, let’s take a look at the main text of this article.
Using 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; } Calculate the number of weeks again:
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){ //For those who are less than 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{ //Return null return null; } } return null; }Summarize
The above is the entire content of using Calendar in Java to calculate the number of days and weeks between two dates. I hope that the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.