Java Code Examples for org.ocpsoft.prettytime.PrettyTime#format()

The following examples show how to use org.ocpsoft.prettytime.PrettyTime#format() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeUtils.java    From OmniList with GNU Affero General Public License v3.0 6 votes vote down vote up
private static String getPrettyTime(Date date, Locale locale) {
    if (date == null) {
        return "";
    }
    PrettyTime pt = new PrettyTime();
    if (locale != null) {
        pt.setLocale(locale);
    }
    return pt.format(date);
}
 
Example 2
Source File: RelativeTime.java    From kaif with Apache License 2.0 6 votes vote down vote up
@Override
public Object exec(List arguments) throws TemplateModelException {
  if (arguments.size() < 1) {
    throw new TemplateModelException("require an Instant as argument");
  }
  if (arguments.size() > 2) {
    throw new TemplateModelException("too many arguments");
  }
  PrettyTime prettyTime = new PrettyTime(Environment.getCurrentEnvironment().getLocale());
  // only support day unit now
  if (arguments.size() == 2 && arguments.get(1).toString().equals("Day")) {
    List<TimeUnit> units = prettyTime.getUnits()
        .stream()
        .filter(timeUnit -> timeUnit.getMillisPerUnit() > new Day().getMillisPerUnit())
        .collect(Collectors.toList());
    units.forEach(prettyTime::removeUnit);
  }

  StringModel stringModel = (StringModel) arguments.get(0);
  Instant instant = (Instant) stringModel.getAdaptedObject(Instant.class);
  return prettyTime.format(Date.from(instant));
}
 
Example 3
Source File: Utils.java    From Hify with MIT License 5 votes vote down vote up
public static String DateToTimeFormat(String oldstringDate){
    PrettyTime p = new PrettyTime(Locale.ENGLISH);
    String isTime = null;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
                Locale.ENGLISH);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = sdf.parse(oldstringDate);
        isTime = p.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return isTime;
}
 
Example 4
Source File: DateTimeUtils.java    From Bus-Tracking-Parent with GNU General Public License v3.0 5 votes vote down vote up
public static String getPreetyTimeString(String this_date) {
    dateFormat = new SimpleDateFormat(DATE_FORMAT_STR);
    date = new Date();
    try {
        date = dateFormat.parse(this_date);
        PrettyTime prettyTime = new PrettyTime();
        return prettyTime.format(date);
    } catch (NullPointerException|ParseException p) {
        L.err("Cant parse date:" + this_date);
        return "";
    }
}
 
Example 5
Source File: DateTimeUtils.java    From mycollab with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String getPrettyDateValue(LocalDateTime dateTime, ZoneId zoneId, Locale locale) {

        if (dateTime == null) {
            return "";
        }
        PrettyTime p = new PrettyTime(locale);
        return p.format(convertLocalDateTimeToDate(dateTime, zoneId));
    }
 
Example 6
Source File: UserUIContext.java    From mycollab with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String formatPrettyTime(LocalDate localDate) {
    if (localDate != null) {
        Date date = Date.from(localDate.atStartOfDay(getUserTimeZone()).toInstant());
        PrettyTime p = new PrettyTime();
        return p.format(date);
    } else {
        return "";
    }
}
 
Example 7
Source File: Flash.java    From LibreNews-Android with GNU General Public License v3.0 4 votes vote down vote up
public String getHumanReadableRelativeTime() {
    PrettyTime p = new PrettyTime();
    return p.format(getDate());
}
 
Example 8
Source File: DateTool.java    From intra42 with Apache License 2.0 4 votes vote down vote up
static public String getDurationAgo(Date date) {
    PrettyTime p = new PrettyTime(Locale.getDefault());
    return p.format(date);
}
 
Example 9
Source File: ClimbSession.java    From climb-tracker with Apache License 2.0 4 votes vote down vote up
public String getPrettyDate() {
    PrettyTime p = new PrettyTime();
    return p.format(getDate());
}
 
Example 10
Source File: MainActivity.java    From glimmr with Apache License 2.0 4 votes vote down vote up
/**
 * An item can be a photo or photoset.
 * An event can be a comment, note, or fav on that item.
 */
private List<Object> buildActivityStream(List<Item> activityItems) {
    List<Object> ret = new ArrayList<Object>();
    if (activityItems == null) {
        return ret;
    }

    PrettyTime prettyTime = new PrettyTime(Locale.getDefault());
    String html = "<small><i>%s</i></small><br>" +
        "%s <font color=\"#ff0084\"><b>%s</b></font> <i>‘%s’</i>";

    for (Item i : activityItems) {
        if ("photo".equals(i.getType())) {
            StringBuilder itemString = new StringBuilder();
            for (int j=i.getEvents().size()-1; j>=0; j--) {
                Event e = ((List<Event>)i.getEvents()).get(j);
                String pTime = prettyTime.format(e.getDateadded());
                String author = e.getUsername();
                if (mUser != null && mUser.getUsername().equals(author)) {
                    author = getString(R.string.you);
                }
                if ("comment".equals(e.getType())) {
                    itemString.append(String.format(html, pTime, author,
                                getString(R.string.commented_on),
                                i.getTitle()));
                } else if ("fave".equals(e.getType())) {
                    itemString.append(String.format(html, pTime, author,
                                getString(R.string.favorited),
                                i.getTitle()));
                } else {
                    Log.e(TAG, "unsupported Event type: " + e.getType());
                    continue;
                }
                if (j > 0) {
                    itemString.append("<br><br>");
                }
            }
            if ( ! itemString.toString().isEmpty()) {
                ret.add(new MenuDrawerActivityItem(itemString.toString(), -1));
            }
        }
    }
    return ret;
}
 
Example 11
Source File: UptimeBean.java    From rpicheck with MIT License 4 votes vote down vote up
public String getRunningPretty() {
    // current time - (secondsRunning * 1000) => start time
    PrettyTime pretty = new PrettyTime();
    return pretty.format(new Date(Calendar.getInstance().getTimeInMillis()
            - (secondsRunning * 1000)));
}
 
Example 12
Source File: TimeFormatter.java    From ServerListPlus with GNU General Public License v3.0 4 votes vote down vote up
public String formatPreciseDuration(Date date) {
    PrettyTime prettyTime = getPrettyTime();
    List<Duration> durations = prettyTime.calculatePreciseDuration(date);
    return prettyTime.format(durations);
}
 
Example 13
Source File: PrettyTimeUtils.java    From es with Apache License 2.0 4 votes vote down vote up
/**
 * 美化时间 如显示为 1小时前 2分钟前
 *
 * @return
 */
public static final String prettyTime(Date date) {
    PrettyTime p = new PrettyTime();
    return p.format(date);

}
 
Example 14
Source File: PrettyTimeUtils.java    From es with Apache License 2.0 4 votes vote down vote up
public static final String prettyTime(long millisecond) {
    PrettyTime p = new PrettyTime();
    return p.format(new Date(millisecond));
}