2017-07-30 9 views
-1

Ich habe zwei String-Variablen "time1" & "time2". Grundsätzlich ist jede String-Variable selbst ein Zeitbereich. Entweder "time1" liegt zwischen "time2" ODER "time2" liegt zwischen "time1". Drucken Sie "Nicht verfügbar".Wie zwei Zeitbereiche in Android zu vergleichen?

Beispiel Fall 1:

String time1 = "14:00 - 16:00" 

String time2 = "15:00 - 16:00" 

Druck "nicht verfügbar"

Beispiel Fall 2:

String time1 = "14:00 - 17:00" 

String time2 = "15:00 - 16:00" 

Druck "nicht verfügbar"

Beispiel Fall 3:

String time1 = "15:00 - 16:00" 

String time2 = "14:00 - 17:00" 

Druck "nicht verfügbar"

Beispiel Fall 4:

String time1 = "15:00 - 16:00" 

String time2 = "14:00 - 16:00" 

Druck "nicht verfügbar"

Beispiel Fall 5:

String time1 = "14:00 - 15:00" 

String time2 = "15:00 - 16:00" 

Print "Verfügbar"

Es könnten mehrere Fälle so sein. bezieht sich eigentlich auf „time2“ „Slot bereits von einem anderen Benutzer während dieser Zeit ausgebucht“ und „time1“ bezieht sich P. S auf „während dieser Zeit neue Benutzer auffordert, slot“: Arbeiten an dem Reservierungssystem.

Antwort

2

Zuerst analysieren Sie die Strings, um startTime und endTime für jede von ihnen zu finden.

Da diese einfach hour:minute sind, können Sie einfach diese als Minute-von-Tag berechnen, d. H. hour * 60 + minute.

Eine einfache Möglichkeit, sowohl die Start- und Endzeiten zu analysieren, wird unter Verwendung von regulären Ausdrücken.

public final class TimeRange { 
    private final int startMinOfDay; 
    private final int endMinOfDay; 

    public TimeRange(String text) { 
     Pattern p = Pattern.compile("(\\d{1,2}):(\\d{2}) - (\\d{1,2}):(\\d{2})"); 
     Matcher m = p.matcher(text); 
     if (! m.matches()) 
      throw new IllegalArgumentException("Invalid time range: " + text); 
     this.startMinOfDay = minOfDay(m.group(1), m.group(2)); 
     this.endMinOfDay = minOfDay(m.group(3), m.group(4)); 
     if (this.endMinOfDay <= this.startMinOfDay) 
      throw new IllegalArgumentException("Invalid time range: " + text); 
    } 
    private static int minOfDay(String hour, String minute) { 
     int h = Integer.parseInt(hour); 
     int m = Integer.parseInt(minute); 
     if (m >= 60 || h >= 24) 
      throw new IllegalArgumentException("Invalid time: " + hour + ":" + minute); 
     return h * 60 + m; 
    } 
    public boolean overlaps(TimeRange that) { 
     return (this.startMinOfDay < that.endMinOfDay && this.endMinOfDay > that.startMinOfDay); 
    } 
} 

-Test

public static void main(String[] args) { 
    test("14:00 - 16:00", "15:00 - 16:00"); 
    test("14:00 - 17:00", "15:00 - 16:00"); 
    test("15:00 - 16:00", "14:00 - 17:00"); 
    test("14:00 - 15:00", "15:00 - 16:00"); 
} 
private static void test(String time1, String time2) { 
    System.out.println(new TimeRange(time1).overlaps(new TimeRange(time2)) ? "Not Available" : "Available"); 
} 

Ausgabe

Not Available 
Not Available 
Not Available 
Available 
+0

thanks man! Es funktionierte :-) – anashamidkh