Pretty Time For GWT

This article has the "Enough Time" library for Java:

How to calculate time ago "in Java?

Is there something similar for GWT?

+7
source share
3 answers
+6
source

I will not recommend you to include 3 parties in it for this task - there is an easier way.

Just count the number of seconds, minutes, hours, ... etc and then format the result text - use Plural Forms - the built-in GWT i18n tool to format the text "one second", "two seconds", etc Thus, all messages will be localized and saved in i18n resources, without any hard code.

+4
source

I encoded this for a java java project. I created a class with two methods:

/** * 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
source

All Articles