判断当前时间是否在某个时间范围内:比如:在8:00~20:00

时间:2022-06-26 17:19:37

//2012/03/12,added by king
   public static void setTime(){
  /*  SimpleDateFormat sdf = new SimpleDateFormat("HHmmss", Locale.CHINA);
    Date m_date = new Date();
    Calendar c = Calendar.getInstance();  
    c.setTime(m_date);    
    long lMill = c.getTimeInMillis();
    c.setTimeInMillis(lMill);
    System.out.println(lMill);
    System.out.print(sdf.format(c.getTime()));*/
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    Calendar currentDate = Calendar.getInstance(); 
    currentDate.setTime(new Date());
   
    Calendar min=Calendar.getInstance();
    //min=currentDate;
    min.set(Calendar.YEAR, currentDate.get(Calendar.YEAR));
    min.set(Calendar.MONTH, currentDate.get(Calendar.MONTH));
    min.set(Calendar.HOUR_OF_DAY, 8);
    min.set(Calendar.MINUTE, 0);
    min.set(Calendar.SECOND, 0);
    min.set(Calendar.MILLISECOND, 0);
   
    Calendar max=Calendar.getInstance();
   // max=currentDate;
    max.set(Calendar.YEAR, currentDate.get(Calendar.YEAR));
    max.set(Calendar.MONTH, currentDate.get(Calendar.MONTH));
    max.set(Calendar.HOUR_OF_DAY, 20);
    max.set(Calendar.MINUTE, 0);
    max.set(Calendar.SECOND, 0);
    max.set(Calendar.MILLISECOND, 0);
   
    if(currentDate.getTimeInMillis()>=min.getTimeInMillis() && currentDate.getTimeInMillis()<=max.getTimeInMillis()){    
     System.out.println("+++++++"+sdf.format(min.getTime()));
     System.out.println(currentDate.getTimeInMillis()-min.getTimeInMillis());
     System.out.println(min.get(Calendar.HOUR_OF_DAY));
     System.out.println(currentDate.get(Calendar.HOUR));
     System.out.println("时间在8点到20点之间");
     currentDate.add(Calendar.HOUR, 8);
     System.out.println("========"+sdf.format(currentDate.getTime()));
    }else{
     System.out.println("-------"+sdf.format(max.getTime()));
     System.out.println("时间不在8点到20点之间");    }
   }