Advertisement
Stranck

DateInRange

Jul 1st, 2018
215
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static boolean validTime() throws ParseException{
  2.         String startTime = "20:30";
  3.         String stopTime = "21:45";
  4.        
  5.         Calendar now = Calendar.getInstance();     
  6.         Calendar start = Calendar.getInstance();
  7.         Calendar stop = Calendar.getInstance();
  8.        
  9.         start.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startTime.split(":")[0]));
  10.         start.set(Calendar.MINUTE, Integer.parseInt(startTime.split(":")[1]));
  11.         stop.set(Calendar.HOUR_OF_DAY, Integer.parseInt(stopTime.split(":")[0]));
  12.         stop.set(Calendar.MINUTE, Integer.parseInt(stopTime.split(":")[1]));
  13.        
  14.         if(start.after(stop)) {
  15.             if(start.after(now)){
  16.                 start.add(Calendar.DATE, -1);
  17.             } else {
  18.                 stop.add(Calendar.DATE, 1);
  19.             }
  20.         }
  21.         return now.after(start) && now.before(stop);
  22. }
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. //version with functions, cuz they are a good thing and idk why the person who asked me that code don't wanted them
  30. public static boolean validTime() throws ParseException{
  31.         String startTime = "20:30";
  32.         String stopTime = "21:45";
  33.        
  34.         Calendar now = Calendar.getInstance();     
  35.         Calendar start = Calendar.getInstance();
  36.         Calendar stop = Calendar.getInstance();
  37.         parse(start, startTime);
  38.         parse(stop, stopTime);
  39.        
  40.         if(start.after(stop)) {
  41.             if(start.after(now)){
  42.                 start.add(Calendar.DATE, -1);
  43.             } else {
  44.                 stop.add(Calendar.DATE, 1);
  45.             }
  46.         }
  47.         return now.after(start) && now.before(stop);
  48.     }
  49.    
  50.     public static void parse(Calendar c, String date){
  51.         c.set(Calendar.HOUR_OF_DAY, getTime(date, 0));
  52.         c.set(Calendar.MINUTE, getTime(date, 1));
  53.     }
  54.    
  55.     public static int getTime(String time, int arg){
  56.         return Integer.parseInt(time.split(":")[arg]);
  57.     }
Advertisement
RAW Paste Data Copied
Advertisement