Salve,
ho due date in formato testo, tipo 221107 e 150607. Dovrei verificare che tra le due non sia passato più di un anno.
Qualcuno mi da un consiglio su come fare?
Grazie
Salve,
ho due date in formato testo, tipo 221107 e 150607. Dovrei verificare che tra le due non sia passato più di un anno.
Qualcuno mi da un consiglio su come fare?
Grazie
Per anno intendi che non siano passati 365 giorni?Originariamente inviato da koala81
ho due date in formato testo, tipo 221107 e 150607. Dovrei verificare che tra le due non sia passato più di un anno.
Andrea, andbin.dev – Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%)
java.util.function Interfaces Cheat Sheet — Java Versions Cheat Sheet
Beh.. converti le stringhe in formato data (utilizzando, ad esempio, GregorianCalendar) e fai i tuoi controlli:
A questo punto effettuerai i tuoi controlli. Ad esempio, se l'anno è uguale sei già a posto e non serve fare nulla, altrimenti crei i due GregorianCalendar e utilizzi il get() passandogli DAY_OF_YEAR, per verificare se è passato un anno:codice:String primaData = "221107"; String secondaData = "150607". int anno1 = Integer.parseInt( primaData.substring(4, 6) ); int anno2 = Integer.parseInt( secondaData.substring(4, 6) ); int mese1 = Integer.parseInt( primaData.substring(2, 4) ); int mese2 = Integer.parseInt( secondaData.substring(2, 4) ); int giorno1 = Integer.parseInt( primaData.substring(0, 2) ); int giorno2 = Integer.parseInt( secondaData.substring(0, 2) );
Ciao.codice:boolean troppo_tempo = false; if (anno1 != anno2) { GregorianCalendat gc1 = new GregorianCalendar(anno1, mese1-1, giorno1); GregorianCalendat gc2 = new GregorianCalendar(anno2, mese2-1, giorno2); int days1 = 0; int days2 = 0; if ( gc1.before(gc2) ) { days1 = gc1.get(gc1.DAY_OF_YEAR); days2 = gc2.get(gc2.DAY_OF_YEAR) + 365; } else { days2 = gc1.get(gc1.DAY_OF_YEAR) + 365; days1 = gc2.get(gc2.DAY_OF_YEAR); } int giorni_trascorsi = days2 - days1; troppo_tempo = (giorni_trascorsi > 365); } return troppo_tempo;![]()
"Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza
Grazie mileeeeeee!!!
Oppure con il solito SimpleDateFormat... insomma di strade ce ne sono.
<´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
"The answer to your question is: welcome to tomorrow"
Io farei:
codice:DateFormat df = new SimpleDateFormat ("ddMMyy"); df.setLenient (false); Date d1 = df.parse ("221107"); Date d2 = df.parse ("150607"); long diff = d1.getTime () - d2.getTime (); if (diff > 365L*86400*1000) { /* sono passati piu` di 365 giorni */ }
Andrea, andbin.dev – Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%)
java.util.function Interfaces Cheat Sheet — Java Versions Cheat Sheet
Forse, per una ragione di "robustezza", conviene ricavare il Calendar da ciascuno, aggiungere un anno al più piccolo (o toglierlo al più grande) tramite l'apposito add, e quindi fare il confronto.Originariamente inviato da andbin
Io farei:
codice:DateFormat df = new SimpleDateFormat ("ddMMyy"); df.setLenient (false); Date d1 = df.parse ("221107"); Date d2 = df.parse ("150607"); long diff = d1.getTime () - d2.getTime (); if (diff > 365L*86400*1000) { /* sono passati piu` di 365 giorni */ }
<´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
"The answer to your question is: welcome to tomorrow"
Che intendi per robustezza??? Tipo gestire l'anno bisestile?
E quale sarebbe la tua soluzione?
A chiunque possa interessare questa è la soluzione che ho adottato...credo che gestisce tutti i casi.
/**
* Verifica se tra due stringhe formattate come YYYYMMDD sia
* trascorso o meno anno.
*
* @param data1: prima data (YYYYMMDD)
* @param data2: seconda data (YYYYMMDD)
* @return booelan: vero se è trascorso un anno, falso altrimenti
*/
public static boolean isSpendedOneYear(String data1 ,String data2){
boolean test = false;
int anno1 = Integer.parseInt( data2.substring(0, 4) );
int anno2 = Integer.parseInt( data1.substring(0, 4) );
int mese1 = Integer.parseInt( data2.substring(4, 6) );
int mese2 = Integer.parseInt( data1.substring(4, 6) );
int giorno1 = Integer.parseInt( data2.substring(6, 8) );
int giorno2 = Integer.parseInt( data1.substring(6, 8) );
Calendar gc1 = new GregorianCalendar(anno1, mese1-1, giorno1);
Calendar gc2 = new GregorianCalendar(anno2, mese2-1, giorno2);
if (gc1.before(gc2)){
gc1.add(Calendar.YEAR, 1);
if (gc1.before(gc2)){
test = true;
}else{
test = false;
}
}else{
gc2.add(Calendar.YEAR, 1);
if (gc2.before(gc1)){
test = true;
}else{
test = false;
}
}
return test;
}
Ciao