8

There is a "Pretty time" library for Java based on this post:

How to calculate "time ago" in Java?

Is there anything like this for GWT?

Community
  • 1
  • 1
quarks
  • 33,478
  • 73
  • 290
  • 513

3 Answers3

6

GWT relative time

hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
4

I will not recommend you to include some 3-rd parties for this task - there is more simple way.

Just count number of seconds, minutes, hours, ...e.t.c. and then format the result text - use Plural Forms - built in GWT i18n tool for formatting such text as "one second", "two seconds", e.t.c. So all messages will be localized and stored into i18n resources, no any hardcode.

Jaroslav Záruba
  • 4,694
  • 5
  • 39
  • 58
0

I coded this for an Android java project. I created a class with 2 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;
        }
    }
}
Faegy
  • 942
  • 1
  • 12
  • 29
Mihai.Mehe
  • 448
  • 8
  • 13