I. Java time comparison requirements
1. From time a to time B, how many years, months and days is the difference.
For example, from February 2, 2011 to March 2, 2017, the results are as follows:
* difference of 6 years, 1 month, 0 day
2. What is the difference between time a and time B, year, month and day?
For example, from February 2, 2011 to March 2, 2017, the results are as follows:
* in years: 6 years
* in months: 73 months
* in days: 2220 days
3. What is the difference between time a and time B
For example: 2011-02-02 to 2017-03-02, the result is about:
* 6.1 years
This demand is reflected in three years and six months since I joined the company, or three and a half years.
Not much, look at the code.
II. Java time code implementation
The code of date comparison object DayCompare uses lombok, which will be mentioned later. If you don't use it, you should write the getter / setter method and the constructor.
@Data @Builder public class DayCompare{ private int year; private int month; private int day; }
2.1 time from A to B, year, month, day.
/** * Calculate the difference between two dates (MM DD YY) * For example: the difference between February 2, 2011 and March 2, 2017 is 6 years, 1 month and 0 day * @param fromDate * @param toDate * @return */ public static DayCompare dayComparePrecise(Date fromDate,Date toDate){ Calendar from = Calendar.getInstance(); from.setTime(fromDate); Calendar to = Calendar.getInstance(); to.setTime(toDate); int fromYear = from.get(Calendar.YEAR); int fromMonth = from.get(Calendar.MONTH); int fromDay = from.get(Calendar.DAY_OF_MONTH); int toYear = to.get(Calendar.YEAR); int toMonth = to.get(Calendar.MONTH); int toDay = to.get(Calendar.DAY_OF_MONTH); int year = toYear - fromYear; int month = toMonth - fromMonth; int day = toDay - fromDay; return DayCompare.builder().day(day).month(month).year(year).build(); }
2.2 what is the difference between time a and time B, year, month and day?
/** * Calculate the difference between the two dates in the units of year, month and day, and what are the respective calculation results * For example: February 2, 2011 to March 2, 2017 * Difference in years: 6 years * In months: 73 months * Difference in days: 2220 days * @param fromDate * @param toDate * @return */ public static DayCompare dayCompare(Date fromDate,Date toDate){ Calendar from = Calendar.getInstance(); from.setTime(fromDate); Calendar to = Calendar.getInstance(); to.setTime(toDate); //As long as years int fromYear = from.get(Calendar.YEAR); int fromMonth = from.get(Calendar.MONTH); int toYear = to.get(Calendar.YEAR); int toMonth = to.get(Calendar.MONTH); int year = toYear - fromYear; int month = toYear * 12 + toMonth - (fromYear * 12 + fromMonth); int day = (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / (24 * 3600 * 1000)); return DayCompare.builder().day(day).month(month).year(year).build(); }
2.3 what is the difference between time a and time B
/** * Calculate the year difference between two dates * Column: the difference between February 2, 2011 and March 2, 2017 is about 6.1 years * @param fromDate * @param toDate * @return */ public static String yearCompare(Date fromDate,Date toDate){ DayCompare result = dayComparePrecise(fromDate, toDate); double month = result.getMonth(); double year = result.getYear(); //Returns 2 decimal places and rounds DecimalFormat df = new DecimalFormat("######0.0"); return df.format(year + month / 12); }
Copyright: SO JSON online parsing
Original address: https://www.sojson.com/blog/260.html