java时间工具 判断时间大于一个月,小于一年,时间必须以月为单位分割(欢迎测试)

  1 package test;
  2 
  3 import java.io.UnsupportedEncodingException;
  4 import java.text.ParseException;
  5 import java.text.SimpleDateFormat;
  6 import java.util.Base64;
  7 import java.util.Calendar;
  8 import java.util.Date;
  9 
 10 public class Test {
 11     public static void main(String[] args) throws UnsupportedEncodingException, ParseException {
 12         /*
 13          * Base64.Encoder encoder = Base64.getEncoder(); Base64.Decoder decoder
 14          * = Base64.getDecoder(); String str="mx..05220416";
 15          * System.out.println(new
 16          * String(decoder.decode("Z2hteDA1MjIwNDE2"),"UTF-8"));
 17          */
 18         /*
 19          * String time ="2018-01-02"; String time2 ="2019-01-02";
 20          * SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd");
 21          * //加上时间 Date begin=sDateFormat.parse(time); Date
 22          * end=sDateFormat.parse(time2); //System.out.println(begin);
 23          * System.out.println(begin.getTime());
 24          * System.out.println(end.getTime()); long oneyear=31536000;
 25          * System.out.println(end.getTime()/1000-begin.getTime()/1000==oneyear);
 26          * System.out.println();
 27          */    
 28         
 29         String begin="2017-02-27";
 30         String end="2017-03-28";
 31         System.out.println(test(begin,end));
 32     }
 33 
 34     public static boolean test(String beg,String ed) throws ParseException{
 35         boolean result=false;
 36         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 37         Date date = format.parse(beg);
 38         Date date2 = format.parse(ed);
 39         // 开始时间
 40         Calendar start = Calendar.getInstance();
 41         start.setTime(date);
 42         // 结束时间
 43         Calendar end = Calendar.getInstance();
 44         end.setTime(date2);
 45         if(testMonth(start,end)&&testYear(date,date2)&&testDay(start,end)){
 46              result=true;
 47         }
 48         return result;
 49     }
 50     
 51     
 52 
 53 public static boolean testDay(Calendar start,Calendar end){//时间必须要以月为单位分割
 54         boolean result=false;
 55         if(start.getActualMaximum(Calendar.DAY_OF_MONTH)==start.get(Calendar.DATE)){//说明这是开始时间是月份的最后一天
 56             result=end.getActualMaximum(Calendar.DAY_OF_MONTH)==end.get(Calendar.DATE);//那么结束时间也需要是月份的最后一天
 57             if(start.get(Calendar.DATE)==end.get(Calendar.DATE)){
 58                 result=true;
 59             }
 60         }
 61         if(start.getActualMaximum(Calendar.DAY_OF_MONTH)>start.get(Calendar.DATE)){//说明这是开始时间不是月份的最后一天
 62             if(start.get(Calendar.DATE)==end.get(Calendar.DATE)){//结束时间和开始时间是不同月份的同一天
 63                 result=true;
 64             }else if(start.get(Calendar.DATE)>end.get(Calendar.DATE)&&end.getActualMaximum(Calendar.DAY_OF_MONTH)==end.get(Calendar.DATE)){
 65                 result=true;
 66             }
 67             else if(start.get(Calendar.DATE)<end.get(Calendar.DATE)&&start.getActualMaximum(Calendar.DAY_OF_MONTH)==start.get(Calendar.DATE)){
 68                 result=true;
 69             }
 70         }
 71         return result;
 72         }
 73     
 74     
 75     public static boolean testYear(Date date,Date date2){
 76         boolean result=false;
 77         if(date2.getTime()/1000-date.getTime()/1000<31536000){//相差的时间不能大于一年
 78             result=true;
 79         }
 80         System.out.println(date2.getTime()/1000-date.getTime()/1000);
 81         return result;
 82     }
 83     
 84     public static boolean testMonth(Calendar start,Calendar end){
 85         boolean result=false;
 86         int subMonthCount = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR) == 0)//相差的月份
 87                 ? end.get(Calendar.MONTH) - start.get(Calendar.MONTH) // 同一年
 88                 : ((end.get(Calendar.YEAR) - start.get(Calendar.YEAR) >= 2) // 年数差超过2年
 89                         ? (end.get(Calendar.YEAR) - start.get(Calendar.YEAR) - 1) * 12
 90                                 + start.getActualMaximum(Calendar.MONTH) - start.get(Calendar.MONTH)
 91                                 + end.get(Calendar.MONTH) + 1
 92                         : start.getActualMaximum(Calendar.MONTH) - start.get(Calendar.MONTH)
 93                                 + end.get(Calendar.MONTH) + 1); // 年数差为1,Calendar.get(MONTH)
 94                                                                 // 第一月是0,所以+1
 95         //System.out.println(subMonthCount);
 96         if(subMonthCount<1){//相差月份必须大于一个月
 97             result=false;
 98         }else{
 99             result=true;
100         }
101         return result;
102     }
103     
104     
105 }


//欢迎测试
posted @ 2018-08-17 15:58  曾将  阅读(7722)  评论(0编辑  收藏  举报
//目录 欢迎评论,一起学习,对大家有用请点个赞。