Entra

View Full Version : [JAVA] Problemi con orario - minuti in int


Andrew28
14-09-2013, 12:23
Ciao a tutti, ho un problema, in pratica nel mio programma c'è una funzione in background che ogni due minuti deve prendere la data attuale(solo i minuti) e sommargli un delay che gli viene passato come argomento all'avvio del programma. Questa nuova data viene assegnata a un oggetto domanda che viene appunto prodotto ogni due minuti e quindi ogni due minuti mi deve stampare una nuova domanda con dataChiusura + delay.
Ecco io l'ho fatto ma ai primi due turni funziona, al terzo mi da una data precedente all'ultima creata, per capire:

Supponendo che il delay sia = 3
avvio programma 13:15
domanda1 dataChiusura: 13:18
-dopodueminuti-
domanda2 dataChiusura: 13:20
-dopodueminuti-
domanda3 dataChiusura: 13:19

e cosi via, magari una volta in positivo e quella dopo in negativo!
Che senso ha? Non riesco a capire perchè debba fare cosi.
Vi posto il codice:

public static String domandaId;
public static GregorianCalendar Calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT+2"));
public static Integer hour;
public static Integer now;
public static Integer dataChiusura;
public static Integer delay;
public static Domanda nuovaDomanda;



public static class MyThread extends java.util.TimerTask {

public void run() { // Contiene il processo da eseguire in background



hour = java.util.Calendar.getInstance().get(java.util.Calendar.HOUR);
Integer rand = delay;
dataChiusura = java.util.Calendar.getInstance().get(java.util.Calendar.MINUTE);
dataChiusura = dataChiusura + rand ; // sommiamo il valore delay a minuti attuali
try
{

FileReader fr = new FileReader( "domande.txt" );
BufferedReader br = new BufferedReader( fr );

// dichiaro la variabile String
String stringRead = br.readLine( );

while( stringRead != null )
{

// uso lo StringTokenizer per scorrere il file suddiviso dai ;
StringTokenizer st = new StringTokenizer( stringRead, ";" );
q = st.nextToken( );
r = st.nextToken( );

try
{
UUID id = UUID.randomUUID();
domandaId = id.toString();

Domanda quiz = new Domanda(q, r, domandaId, dataChiusura);

// aggiungo la domanda alla lista domande
domande.add( quiz );
}

catch( NumberFormatException nfe )
{
System.out.println( "Errore in domande: " + stringRead + "; record");
}

// legge la linea
stringRead = br.readLine( );
}

// chiude il flusso con "domande.txt"
br.close( );
}
catch( FileNotFoundException fnfe )
{
System.out.println( "400 File domande.txt non trovato" );
}

catch( IOException ioe )
{
ioe.printStackTrace( );
}
//questo blocco serve a controllare che vengano pescate sempre domande diverse
if(domandeCorrenti.isEmpty()){
nuovaDomanda = domande.get((int)(Math.random()*domande.size()));
domandeCorrenti.add(nuovaDomanda);
}
else{
boolean errore;
do{
errore = false;
nuovaDomanda = domande.get((int)(Math.random()*domande.size()));
for (int i=0; i<domandeCorrenti.size(); i++){
if (domandeCorrenti.get(i).getq().equals(nuovaDomanda.getq())){
errore = true;
break;
}
}
}while(errore==true);

if (errore==false) {
domandeCorrenti.add(nuovaDomanda);
}
}
//fino a qui
System.out.printf("ECCO:" + domandeCorrenti + "\n\n");

} // run()
}




Grazie in anticipo a chiunque riesca a darmi qualche suggerimento!

fally
18-09-2013, 15:50
il sorgente è incompleto, potresti postare l'oggetto Domanda?