EzDevInfo.com

prettytime

Social Style Date and Time Formatting for Java PrettyTime - Elapsed Timestamp Formatting and Conversion for Java (Social, JSF2) | OCPsoft convert timestamp objects into human readable strings, for the java language.

Using the pretty time library to show a time in a human readable format in JSF

I happened to use pretty time 1.0.6 to display an interval between two points in time in a human readable format.

There is an in-built JSF converter,

com.ocpsoft.pretty.time.web.jsf.PrettyTimeConverter

but it supports only java.util.Date objects.

I happened to use the following converter for org.joda.time.DateTime

@ManagedBean
@ApplicationScoped
public final class PrettyJodaTimeConverter extends PrettyTimeConverter
{
    @Override
    public String getAsString(final FacesContext context, final UIComponent component, final Object value)
    {
        if (value instanceof DateTime) {
            return super.getAsString(context, component, ((DateTime) value).toDate());
        } else {
            return super.getAsString(context, component, value);
        }
    }
}

This displays an interval, for example, "4 hours ago" on <h:outputText>, for example

<h:outputText value="#{productInquiryManagedBean.lastInquiry}"
              converter="#{prettyJodaTimeConverter}"/>

But I need to display a message on <p:growl> (or <p:messages>, <h:messages>) based on a conditional check in a JSF managed bean. Therefore, this converter cannot be used (please suggest, if it can be used). Instead, I need to manually format it in the managed bean in question like so,

private DateTime lastInquiry;
private String emailId;
private Product product;

if(productInquiryService.isPeriodExpired(30, emailId, product))
{
    lastInquiry=productInquiryService.getDateTimeOfLastInquiry(email, product);

    Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
    PrettyTime time=new PrettyTime(lastInquiry.toDate(), locale);
    time.setLocale(locale);

    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, Utility.getMessage("faces.message.error"), Utility.getMessage("inquiry.min.time.expire", time.format(new Date()))));
}

This displays a message on <p:growl> like - "You made an inquiry 4 hours from now".

Can this message format be changed to "You made an inquiry 4 hours ago" exactly as displayed on <h:outputText> as shown above?

Also, the interval format displayed here is not localized to a particular locate from a resource bundle. It always seems to be using its default locale. The message produced by PrettyTime#format() should be localized.


I was passing wrong parameters to the constructor. Hence the wrong message indicating a future time. They should be like as follows.

PrettyTime time=new PrettyTime(new Date(), locale);
time.format(lastInquiry.toDate());
//lastInquiry is fetched from the database which a customer made an inquiry on.

It now displays the correct (past time) format like "4 hours ago".

Regarding the locale I was looking for was HI (actually hi_IN, Hindi in India) is not available in the resource bundles (in the com.ocpsoft.pretty.time.i18n package) supplied in the library even though it is mentioned there in the i18n and multiple-languages support section.


Source: (StackOverflow)

Grails and Angular: GSP plugin tag inside ng-repeat loop

I'm working in a Grails project that uses Angular.

I use a plugin called PrettyPrint in order to have "twitter like" time (i.e. formatting dates like 'moments ago').

I want to use the plugin inside a ng-repeat loop.

<tr ng-repeat="notification in notifications">
...
    <td><prettytime:display date="${notification.date}" /></td>
...
</tr>

In the above code, the error thrown is Cannot get property 'date' on null object. (it doesn't recognize notification item from angular loop).

If I use {{notification.date}} it shows the date.

How can I deal with both, the plugin and Angular?


Source: (StackOverflow)

Advertisements

Converting timestamp from parse.com in java

I'm getting my object's createdAt timestamp back from parse.com as 2014-08-01T01:17:56.751Z. I have a class that converts it to relative time.

public static String timeAgo(String time){
  PrettyTime mPtime = new PrettyTime();

  long timeAgo = timeStringtoMilis(time);

  return mPtime.format( new Date( timeAgo ) );
}

public static long timeStringtoMilis(String time) {
  long milis = 0;

  try {
    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date   = sd.parse(time);
    milis       = date.getTime();
  } catch (Exception e) {
    e.printStackTrace();
  }

  return milis;
}

The problem is that this parses the date wrongly. Right now the result says 4 decades ago and this very wrong. What I'm I doing wrong?


Source: (StackOverflow)

Parsing ill-formated date-time string in Java

In one sentence: Should I be able to parse "04 Dec 2014 pm 1:58" by PrettyTime?

Descriptive: I am in need to parse & get proper date format from some ill formatted date-time string. e.g. "04 Dec 2014 pm 1:58". When I do parse this example string, I get : "Thu Dec 04 02:59:33 ALMT 2014" which is I believe my current time stamp.

Consideration: if I had only this single ill-format, I could write my SimpleDateFormat. But there can a good varieties of formatting which are mostly going to be ill-formatted.

Can any of you kindly tell me, whether should I expect PrettyTime to parse for this type of ill-formatted string? Or could you please point to any Java library which can handle these type of ill formatted date strings in Java?


Source: (StackOverflow)

How to describe duration in Android?

I'm writing small app and I need to write duration of sport event in i18n. Im using PrettyTime library for date, but when I attempt to use DateUtils or PrettyTime, I have issues..

For example I want to say that duration is 2 minutes. I need some way to pass it to library which supports i18n and accept milliseconds and return Chars.

In android we have:

com.android.internal.R.plurals.duration_minutes

But I can't access to it from my App. Is there any way to make it using correct way and not writing own plurals for all languages?

Thank you


Source: (StackOverflow)