Example usage for java.text DateFormat getDateInstance

List of usage examples for java.text DateFormat getDateInstance

Introduction

In this page you can find the example usage for java.text DateFormat getDateInstance.

Prototype

public static final DateFormat getDateInstance(int style, Locale aLocale) 

Source Link

Document

Gets the date formatter with the given formatting style for the given locale.

Usage

From source file:org.openmrs.util.Format.java

public static String format(Date date, Locale locale, FORMAT_TYPE type) {
    log.debug("Formatting date: " + date + " with locale " + locale);

    DateFormat dateFormat = null;

    if (type == FORMAT_TYPE.TIMESTAMP) {
        dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
    } else if (type == FORMAT_TYPE.TIME) {
        dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
    } else {/* ww w  . j a v  a 2 s  .  c om*/
        dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    }
    return date == null ? "" : dateFormat.format(date);
}

From source file:org.nuxeo.ecm.social.mini.message.MiniMessageHelper.java

public static String toJSON(PageProvider<MiniMessage> pageProvider, Locale locale, CoreSession session) {
    try {/* w  w  w  .ja v a2  s  . c  o m*/
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);

        List<Map<String, Object>> miniMessages = new ArrayList<Map<String, Object>>();
        for (MiniMessage miniMessage : pageProvider.getCurrentPage()) {
            Map<String, Object> o = new HashMap<String, Object>();
            o.put("id", miniMessage.getId());
            o.put("actor", miniMessage.getActor());
            o.put("displayActor", miniMessage.getDisplayActor());
            o.put("message", miniMessage.getMessage());
            o.put("publishedDate", dateFormat.format(miniMessage.getPublishedDate()));
            o.put("isCurrentUserMiniMessage", session.getPrincipal().getName().equals(miniMessage.getActor()));
            miniMessages.add(o);
        }

        Map<String, Object> m = new HashMap<String, Object>();
        m.put("offset", ((AbstractActivityPageProvider) pageProvider).getNextOffset());
        m.put("limit", pageProvider.getCurrentPageSize());
        m.put("miniMessages", miniMessages);

        ObjectMapper mapper = new ObjectMapper();
        StringWriter writer = new StringWriter();
        mapper.writeValue(writer, m);

        return writer.toString();
    } catch (Exception e) {
        throw new ClientRuntimeException(e);
    }
}

From source file:de.interseroh.report.formatters.DateFormatter.java

@Override
protected DateFormat getFormatterInstance(Locale locale) {
    return DateFormat.getDateInstance(DateFormat.SHORT, locale);
}

From source file:org.openvpms.web.resource.i18n.format.DateFormatter.java

/**
 * Returns a date format.//w w w.ja  v  a 2 s.c om
 *
 * @param edit if {@code true} return a format for editing otherwise return a format for viewing dates
 * @return a date format
 */
public static DateFormat getDateFormat(boolean edit) {
    DateFormat format;
    Locale locale = Messages.getLocale();
    String pattern = (edit) ? DATE_EDIT_PATTERN : DATE_VIEW_PATTERN;
    if (pattern == null) {
        if (edit) {
            // specify SHORT style when parsing, so that 2 digit years
            // are handled correctly
            format = DateFormat.getDateInstance(DateFormat.SHORT, locale);
        } else {
            format = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
        }
    } else {
        format = new SimpleDateFormat(pattern, locale);
    }
    return format;
}

From source file:fr.paris.lutece.plugins.grusupply.service.GruSupplyPlugin.java

/**
 * {@inheritDoc}/*from ww  w.  j  a va2  s  . co m*/
 */
@Override
public void init() {
    BeanUtilsBean.getInstance().getConvertUtils().register(
            new DateConverter(DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRANCE)),
            java.sql.Date.class);
}

From source file:DateFormatDemo.java

static public void showDateStyles(Locale currentLocale) {

    Date today = new Date();
    String result;/*www .java2  s.  c  om*/
    DateFormat formatter;

    int[] styles = { DateFormat.DEFAULT, DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG,
            DateFormat.FULL };

    System.out.println();
    System.out.println("Locale: " + currentLocale.toString());
    System.out.println();

    for (int k = 0; k < styles.length; k++) {
        formatter = DateFormat.getDateInstance(styles[k], currentLocale);
        result = formatter.format(today);
        System.out.println(result);
    }
}

From source file:org.openmrs.web.taglib.ActiveListWidget.java

public int doStartTag() {
    UserContext userContext = Context.getUserContext();

    Locale loc = userContext.getLocale();
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc);

    Set<Concept> addConceptList = OpenmrsUtil.conceptSetHelper(addConcept);
    Set<Concept> removeConceptList = OpenmrsUtil.conceptSetHelper(removeConcept);
    List<Concept> otherConceptList = OpenmrsUtil.conceptListHelper(otherGroupedConcepts);

    boolean doObsGroups = otherConceptList.size() > 0;

    if (onDate == null)
        onDate = new Date();

    // maps Concept to the date that became active
    Map<Concept, Obs> activeList = new HashMap<Concept, Obs>();
    for (Obs o : observations) {
        // skip observations in the future
        if (OpenmrsUtil.compare(o.getObsDatetime(), onDate) > 0)
            continue;
        Concept c = o.getConcept();//from w w w. ja  v  a 2s.  co m
        Concept toDo = o.getValueCoded();
        if (toDo == null)
            toDo = c;
        if (addConceptList.contains(o.getConcept())) {
            Date newActiveDate = o.getObsDatetime();
            Obs tmp = activeList.get(c);
            Date currentActiveDate = tmp == null ? null : tmp.getObsDatetime();
            if (currentActiveDate == null || newActiveDate.compareTo(currentActiveDate) < 0)
                activeList.put(toDo, o);
        } else if (removeConceptList.contains(o.getConcept())) {
            activeList.remove(toDo);
        }
    }
    List<Map.Entry<Concept, Obs>> ordered = new ArrayList<Map.Entry<Concept, Obs>>(activeList.entrySet());
    Collections.sort(ordered, new Comparator<Map.Entry<Concept, Obs>>() {

        public int compare(Map.Entry<Concept, Obs> left, Map.Entry<Concept, Obs> right) {
            return left.getValue().getObsDatetime().compareTo(right.getValue().getObsDatetime());
        }
    });

    Map<Obs, Collection<Obs>> obsGroups = new HashMap<Obs, Collection<Obs>>();
    if (doObsGroups) {
        ObsService os = Context.getObsService();
        for (Obs o : activeList.values())
            if (o.isObsGrouping())
                obsGroups.put(o, o.getGroupMembers());
    }

    StringBuilder sb = new StringBuilder();
    String before = "";
    String after = "";
    String obsGroupHeader = "";
    String beforeItem = "";
    String afterItem = "";
    String obsGroupItemSeparator = "";

    if ("ol".equals(displayStyle) || "ul".equals(displayStyle)) {
        before = "<" + displayStyle + ">";
        after = "</" + displayStyle + ">";
        beforeItem = "<li>";
        afterItem = "</li>";
        obsGroupItemSeparator = ", ";

    } else if (displayStyle.startsWith("separator:")) {
        afterItem = displayStyle.substring(displayStyle.indexOf(":") + 1);
        obsGroupItemSeparator = " ";

    } else if ("table".equals(displayStyle)) {
        before = "<table>";
        after = "</table>";
        beforeItem = "<tr><td>";
        afterItem = "</td></tr>";
        obsGroupItemSeparator = "</td><td>";
        if (doObsGroups) {
            StringBuilder s = new StringBuilder();
            s.append("<tr><th></th>");
            for (Concept c : otherConceptList) {
                ConceptName cn = c.getBestShortName(loc);
                s.append("<th><small>" + cn.getName() + "</small></th>");
            }
            s.append("</tr>");
            obsGroupHeader = s.toString();
        }

    } else {
        throw new RuntimeException("Unknown displayStyle: " + displayStyle);
    }

    if (ordered.size() > 0) {
        sb.append(before);
        sb.append(obsGroupHeader);
        for (Map.Entry<Concept, Obs> e : ordered) {
            sb.append(beforeItem);
            sb.append(e.getKey().getName(loc, false).getName());
            if (showDate)
                sb.append(" ").append(df.format(e.getValue().getObsDatetime()));
            if (doObsGroups) {
                Collection<Obs> obsGroup = obsGroups.get(e.getValue());
                for (Concept c : otherConceptList) {
                    sb.append(obsGroupItemSeparator);
                    if (obsGroup != null) {
                        for (Obs o : obsGroup) {
                            if (c.equals(o.getConcept())) {
                                sb.append(o.getValueAsString(loc));
                                break;
                            }
                        }
                    }
                }
            }
            sb.append(afterItem);
        }
        sb.append(after);
    }

    try {
        JspWriter w = pageContext.getOut();
        w.println(sb);
    } catch (IOException ex) {
        log.error("Error while writing to JSP", ex);
    }

    return SKIP_BODY;
}

From source file:DateUtil.java

/**
 * Parses given string according to specified locale and date style
 *
 * @param source    Source string to parse date from
 * @param locale    Locale to use for parsing date
 * @param dateStyle Date style/*from  w w w. ja v a  2  s  . c om*/
 * @return Date object corresponding to representation given in source string
 * @throws ParseException if given string could not be properly parsed according to given locale and style
 * @see java.text.DateFormat
 */
public static Date parseDate(String source, Locale locale, int dateStyle) throws ParseException {
    DateFormat formatter = DateFormat.getDateInstance(dateStyle, locale);
    return formatter.parse(source);
}

From source file:de.odysseus.calyxo.base.util.ParseUtils.java

private static Date parseDate(String value) throws ParseException {
        DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale);
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        return (Date) parse(format, value);
    }/*from www . j a  v  a 2 s  .  c  o m*/

From source file:org.sonar.server.charts.jruby.TrendsChart.java

public TrendsChart(int width, int height, String localeKey, boolean displayLegend) {
    super(width, height);
    this.displayLegend = displayLegend;
    seriesById = new TreeMap<Long, TimeSeries>();
    plot = new XYPlot();
    DateAxis dateAxis = new DateAxis();
    dateAxis.setDateFormatOverride(// ww  w  .j a  v a  2 s . c  o m
            DateFormat.getDateInstance(DateFormat.SHORT, LocaleUtils.toLocale(localeKey)));
    plot.setDomainAxis(dateAxis);
}