C'è una biblioteca "Pretty tempo" per Java in base a questo post:"Abbastanza tempo" per GWT
How to calculate "time ago" in Java?
C'è qualcosa di simile per GWT?
C'è una biblioteca "Pretty tempo" per Java in base a questo post:"Abbastanza tempo" per GWT
How to calculate "time ago" in Java?
C'è qualcosa di simile per GWT?
Non vi consiglio di includere alcuni partiti 3-rd per questo compito - non c'è modo più semplice.
Basta contare il numero di secondi, minuti, ore, ... e.t.c. e quindi formattare il testo del risultato - utilizzare Plural Forms - strumento GWT i18n incorporato per formattare il testo come "un secondo", "due secondi", e.t.c. Quindi tutti i messaggi saranno localizzati e memorizzati in risorse i18n, nessun hardcode.
L'ho codificato per un progetto java Android. Ho creato una classe con 2 metodi:
/**
* Created by mihai on 2/27/17.
*/
import android.util.Log;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.ocpsoft.prettytime.PrettyTime;
public class Ptime {
static public class PtimeFormatter {
public static String getPtime(){
PrettyTime p = new PrettyTime();
Log.d("demo", "time now: "+new Date());
//getPtimeFrom("Mon Feb 27 19:17:13 EST 2017");
return p.format(new Date());
//prints: “moments from now”
//System.out.println(p.format(new Date(System.currentTimeMillis() + 1000 * 60 * 10)));
//prints: “10 minutes from now”
}
public static String getPtimeFrom(String t){
PrettyTime p = new PrettyTime();
DateFormat formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
try {
Date date = (Date)formatter.parse(t);
Log.d("demo", "time from now: "+p.format(date));
return p.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
//Log.d("demo", "time now: "+p.format(date));
return null;
}
public static String getPTimeMillis(String t){
PrettyTime p = new PrettyTime();
String currMilis = String.valueOf(System.currentTimeMillis());
DateFormat formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
try {
Date aTime = formatter.parse(t);
Log.d("demo", "time millis: "+aTime.getTime());
return String.valueOf(Long.parseLong(currMilis) - aTime.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
}
Uso: Ptime.PtimeFormatter.getPtimeFrom ("stringDate"); –
Il metodo getPTimeMillis ("stringDate"); può essere usato per confrontare l'ordine di 2 eventi che si verificano in diversi punti nel tempo per il numero di millisecondi. –
C'è anche una lib con più di due impostazioni locali? – confile