2012-01-21 13 views

risposta

4

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.

0

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; 
     } 
    } 
} 
+0

Uso: Ptime.PtimeFormatter.getPtimeFrom ("stringDate"); –

+0

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. –