JAVA judges whether the time span of the two parameters reaches year/month/day. Use of JAVA Calendar class

When the project meets the needs,
Insert picture description here
optimize the chart when the user query time span is too large, and the chart is too crowded: the X-axis value of echarts should be changed according to the user query time period.

查询时间段达到年时X轴对应显示每月值;
查询时间段达到月时X轴对应显示每天值;
查询时间段达到天时X轴对应显示每小时值;
查询时间段达不到天时X轴显示默认时间字段;

Need to write logic to determine the time span of the query, the logic is as follows:

public int getYear(Date TIME,Date TIMEEnd) {
    
    
//		调用方法前判断日期是否为空   判断查询时间跨度是否达到年(12-N月)
       Calendar cal1 = Calendar.getInstance();
       cal1.setTime(TIME);
       Calendar cal2 = Calendar.getInstance();
       cal2.setTime(TIMEEnd);
       
       int Year = cal1.get(Calendar.YEAR);
       int endYear = cal2.get(Calendar.YEAR);
       int Month = cal1.get(Calendar.MONTH)+1;
       int endMonth = cal2.get(Calendar.MONTH)+1;
       int day = cal1.get(Calendar.DAY_OF_MONTH);
       int endday = cal2.get(Calendar.DAY_OF_MONTH);
       if (Year == endYear) {
    
    //同年      调用方法前判断日期是否为空   判断查询时间跨度是否达到月但达不到年(0-364天)
    	   if (Month == endMonth) {
    
    //同年同月      调用方法前判断日期是否为空   判断查询时间跨度是否达到天但达不到月(0-744小时)  达不到天则用PRODUCE_TIME做区分
    		   if (endday == day) {
    
    //同年同月同日
   	            return 0;
   	        	}
   	            return 100;//同年同月不同日
           }
           return 100;//同年不同月
       }
   	   if (Month == endMonth) {
    
    //不同年同月
   		   if (endday >= day) {
    
    //不同年同月 跨度达到年
   			   return 10000;
  	       }
   		   	   return 100;//不同年同月 跨度达不到到年
       }//不同年不同月
   	   if (endMonth > Month) {
    
    //不同年不同月 跨度达到年
   		   return 10000;
   	   }
   	   	   return 100;//不同年不同月 跨度达到月
   }

You can modify the return value according to your needs.
Finally, perform the test and write down the test results of simple test cases to break points:Insert picture description here

In this way, the time span can be judged and the chart data style can be modified according to different situations.

Guess you like

Origin blog.csdn.net/Beatingworldline/article/details/113391656