Originariamente inviato da Kilin
ciao, grazie per la risposta...
purtroppo non basta: prova a sostituire 2:30 invece di 23:30... qui non si accorge che anche 2:30 > 23:30... stavo pensando quando dovremmo sommare 24 all'ora corrente per coprire questo caso..
Una curiosità. E se hai:
02:30 del 04/07/10
20:30 del 04/07/10
05:00 del 05/07/10 (glielo forziamo noi aggiungendo +24)
che fai? Tu dici che non funziona però secondo me non hai posto bene il problema. Le 02.30 del giorno indicato NON sono nella fascia oraria specificata.
Senza indicare il giorno la situazione è ambigua quindi devi forzare un comportamento.
Cosa scegli di fare in questi casi?
edit: Per risparmiare tempo, ti ho aggiunto l'altro if per fare esattamente quello che dici tu
(20:30 <= 02:30 <= 05:00 = true in pratica). In più ti ho creato un metodo che logga i test, così puoi guardare se, in generale, fa quello che deve. Altre casistiche non ne vedo, però non si sa mai.
Codice PHP:
package test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
public class Main
{
private static String format(GregorianCalendar time)
{
String hh = String.valueOf(time.get(Calendar.HOUR_OF_DAY));
String mm = String.valueOf(time.get(Calendar.MINUTE));
if(hh.length() == 1) hh = "0" + hh;
if(mm.length() == 1) mm = "0" + mm;
return hh + ":" + mm;
}
private static String format(boolean b)
{
if(b) return " true";
return "false";
}
private static void log(GregorianCalendar inizio, GregorianCalendar fine, GregorianCalendar ora)
throws IOException
{
File logFile = new File("log.txt");
FileWriter fw = new FileWriter(logFile, true);
String out = format(inizio) + " <= " + format(ora) + " <= " + format(fine) + "\t\t= " + format((ora.after(inizio) && ora.before(fine)));
fw.write(out + "\n");
System.out.println(out);
fw.flush();
fw.close();
}
private static void test() throws IOException
{
Random r = new Random();
for(int i = 0; i < 100; i++)
{
int hh1 = r.nextInt(24);
int hh2 = r.nextInt(24);
int hh3 = r.nextInt(24);
hh3 = (hh3 < hh1 && hh3 < hh2)? hh3+24: hh3;
hh2 = hh1 > hh2 ? hh2+24 : hh2;
GregorianCalendar inizio = new GregorianCalendar(0,0,0, hh1, r.nextInt(60));
GregorianCalendar fine = new GregorianCalendar(0,0,0, hh2, r.nextInt(60));
GregorianCalendar ora = new GregorianCalendar(0,0,0, hh3, r.nextInt(60));
log(inizio, fine, ora);
}
}
public static void main(String[] args)
throws IOException
{
int hh1 = 20;
int hh2 = 5;
int hh3 = 2;
hh3 = (hh3 < hh1 && hh3 < hh2)? hh3+24: hh3;
hh2 = hh1 > hh2 ? hh2+24 : hh2;
GregorianCalendar inizio = new GregorianCalendar(0,0,0, hh1, 30);
GregorianCalendar fine = new GregorianCalendar(0,0,0, hh2, 0);
GregorianCalendar ora = new GregorianCalendar(0,0,0, hh3, 30);
String out = format(inizio) + " <= " + format(ora) + " <= " + format(fine) + "\t\t= " + format((ora.after(inizio) && ora.before(fine)));
System.out.println(out);
test();
}
}