Example usage for java.text SimpleDateFormat toPattern

List of usage examples for java.text SimpleDateFormat toPattern

Introduction

In this page you can find the example usage for java.text SimpleDateFormat toPattern.

Prototype

public String toPattern() 

Source Link

Document

Returns a pattern string describing this date format.

Usage

From source file:org.nuxeo.ecm.platform.ui.web.tag.fn.Functions.java

/**
 * Return the date format to handle date taking the user's locale into account.
 *
 * @since 5.9.1//from  www.j  av  a  2 s.com
 */
public static String dateFormatter(String formatLength) {
    // A map to store temporary available date format
    FacesContext context = FacesContext.getCurrentInstance();
    Locale locale = context.getViewRoot().getLocale();

    int style = DateFormat.SHORT;
    String styleString = mapOfDateLength.get(formatLength.toLowerCase());
    boolean addCentury = false;
    if ("shortWithCentury".toLowerCase().equals(styleString)) {
        addCentury = true;
    } else {
        style = Integer.parseInt(styleString);
    }

    DateFormat aDateFormat = DateFormat.getDateInstance(style, locale);

    // Cast to SimpleDateFormat to make "toPattern" method available
    SimpleDateFormat format = (SimpleDateFormat) aDateFormat;

    // return the date pattern
    String pattern = format.toPattern();

    if (style == DateFormat.SHORT && addCentury) {
        // hack to add century on generated pattern
        pattern = YEAR_PATTERN.matcher(pattern).replaceAll("yyyy");
    }
    return pattern;
}

From source file:org.nuxeo.ecm.platform.ui.web.tag.fn.Functions.java

/**
 * Return the date format to handle date and time taking the user's locale into account.
 *
 * @since 5.9.1//from   w  w w. j  a  v  a  2  s  . c  om
 */
public static String dateAndTimeFormatter(String formatLength) {

    // A map to store temporary available date format

    FacesContext context = FacesContext.getCurrentInstance();
    Locale locale = context.getViewRoot().getLocale();

    int style = DateFormat.SHORT;
    String styleString = mapOfDateLength.get(formatLength.toLowerCase());
    boolean addCentury = false;
    if ("shortWithCentury".toLowerCase().equals(styleString)) {
        addCentury = true;
    } else {
        style = Integer.parseInt(styleString);
    }

    DateFormat aDateFormat = DateFormat.getDateTimeInstance(style, style, locale);

    // Cast to SimpleDateFormat to make "toPattern" method available
    SimpleDateFormat format = (SimpleDateFormat) aDateFormat;

    // return the date pattern
    String pattern = format.toPattern();

    if (style == DateFormat.SHORT && addCentury) {
        // hack to add century on generated pattern
        pattern = YEAR_PATTERN.matcher(pattern).replaceAll("yyyy");
    }
    return pattern;
}

From source file:org.mifos.framework.util.helpers.DateUtils.java

public static java.sql.Date getLocaleDate(Locale locale, String value) throws InvalidDateException {
    // the following line is for 1.1 release and will be removed when date
    // is localized
    locale = internalLocale;//w  w  w . j  a va  2 s .  co m
    java.sql.Date result = null;
    if (locale != null && StringUtils.isNotBlank(value)) {
        SimpleDateFormat shortFormat = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale);
        shortFormat.setLenient(false);
        String userPattern = shortFormat.toPattern();
        String dbDate = convertUserToDbFmt(value, userPattern);
        result = java.sql.Date.valueOf(dbDate);
    }
    return result;
}

From source file:org.mifos.framework.util.helpers.DateUtils.java

public static String getUserLocaleDate(Locale locale, String databaseDate) {
    // the following line is for 1.1 release and will be removed when date
    // is localized
    locale = internalLocale;/*w  w  w .j ava 2 s  .c o  m*/
    if (locale != null && databaseDate != null && !databaseDate.equals("")) {
        try {
            SimpleDateFormat shortFormat = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT,
                    locale);
            String userfmt = convertToCurrentDateFormat(shortFormat.toPattern());
            return convertDbToUserFmt(databaseDate, userfmt);
        } catch (FrameworkRuntimeException e) {
            throw e;
        } catch (Exception e) {
            System.out.println("databaseDate=" + databaseDate + ", locale=" + locale);
            throw new FrameworkRuntimeException(e);
        }
    } else {
        return "";
    }
}

From source file:org.mifos.framework.util.helpers.DateUtils.java

public static String getUserLocaleDate(String databaseDate) {
    if (internalLocale != null && databaseDate != null && !databaseDate.equals("")) {
        try {//from w  w w .  j av a 2  s .  c o m
            SimpleDateFormat shortFormat = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT,
                    internalLocale);
            String userfmt = convertToCurrentDateFormat(shortFormat.toPattern());
            return convertDbToUserFmt(databaseDate, userfmt);
        } catch (FrameworkRuntimeException e) {
            throw e;
        } catch (Exception e) {
            System.out.println("databaseDate=" + databaseDate + ", locale=" + internalLocale);
            throw new FrameworkRuntimeException(e);
        }
    } else {
        return "";
    }
}

From source file:org.mifos.framework.util.helpers.DateUtils.java

public static java.sql.Date getLocaleDate(String value) {
    if (internalLocale != null && value != null && !value.equals("")) {
        try {/*from   ww  w .j a v a2s  . c  om*/
            SimpleDateFormat shortFormat = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT,
                    internalLocale);
            shortFormat.setLenient(false);
            String userPattern = shortFormat.toPattern();
            String dbDate = convertUserToDbFmt(value, userPattern);
            return java.sql.Date.valueOf(dbDate);
        } catch (RuntimeException alreadyRuntime) {
            throw alreadyRuntime;
        } catch (Exception e) {
            throw new FrameworkRuntimeException(e);
        }
    } else {
        return null;
    }
}

From source file:ispok.converter.BirthDateConverter.java

@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o) {

    Locale locale = fc.getViewRoot().getLocale();
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);

    SimpleDateFormat sdf = (SimpleDateFormat) df;
    String pattern = sdf.toPattern();

    pattern = pattern.replace("yy", "yyyy");

    Date date = (Date) o;/*from  ww w . j  a  v a 2 s.  co m*/

    sdf.applyPattern(pattern);

    return sdf.format(date);
}

From source file:ca.sqlpower.dao.session.FormatConverter.java

public String convertToSimpleType(Format convertFrom, Object... additionalInfo) {
    if (convertFrom instanceof DecimalFormat) {
        DecimalFormat decimal = (DecimalFormat) convertFrom;
        return DecimalFormat.class.getSimpleName() + DELIMITER + decimal.toPattern();
    } else if (convertFrom instanceof SimpleDateFormat) {
        SimpleDateFormat date = (SimpleDateFormat) convertFrom;
        return SimpleDateFormat.class.getSimpleName() + DELIMITER + date.toPattern();
    } else {//from   ww w .j a v a 2s .  c o  m
        throw new IllegalStateException("Unknown format to convert from " + convertFrom.getClass());
    }
}

From source file:org.openmrs.module.amrsreports.web.controller.QueuedAMRSReportsPortletController.java

/**
 * @see org.openmrs.web.controller.PortletController#populateModel(javax.servlet.http.HttpServletRequest,
 *      java.util.Map)/*from   w w w  .j ava2 s . co m*/
 */
@Override
protected void populateModel(HttpServletRequest request, Map<String, Object> model) {

    String status = (String) model.get("status");

    Map<MOHFacility, List<QueuedReport>> queuedReportsMap = new LinkedHashMap<MOHFacility, List<QueuedReport>>();
    UserFacilityService userFacilityService = Context.getService(UserFacilityService.class);

    if (Context.isAuthenticated() && status != null) {

        User currentUser = Context.getAuthenticatedUser();

        List<MOHFacility> relevantFacilities = userFacilityService.getAllowedFacilitiesForUser(currentUser);

        List<QueuedReport> reports = null;
        if (relevantFacilities != null && !relevantFacilities.isEmpty()) {
            reports = Context.getService(QueuedReportService.class)
                    .getQueuedReportsByFacilities(relevantFacilities, status);
        } else {
            reports = Context.getService(QueuedReportService.class).getQueuedReportsWithStatus(status);
        }

        for (QueuedReport thisReport : reports) {

            MOHFacility thisMohFacility = thisReport.getFacility();

            if (!queuedReportsMap.containsKey(thisMohFacility))
                queuedReportsMap.put(thisMohFacility, new ArrayList<QueuedReport>());

            queuedReportsMap.get(thisMohFacility).add(thisReport);
        }
    }

    model.put("queuedReportsMap", queuedReportsMap);

    // date time format -- needs to come from here because we can make it locale-specific
    // TODO extract this to a utility if used more than once

    SimpleDateFormat sdf = Context.getDateFormat();
    String format = sdf.toPattern();
    format += " hh:mm a";

    model.put("datetimeFormat", format);
}

From source file:ispok.converter.BirthDateConverter.java

@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String string) {

    logger.trace("Entering getAsObject()");

    Locale locale = fc.getViewRoot().getLocale();

    ResourceBundle rb = ResourceBundle.getBundle("ispok/pres/inter/ispok", locale);

    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    SimpleDateFormat sdf = (SimpleDateFormat) df;

    String pattern = sdf.toPattern();
    String localPattern = sdf.toLocalizedPattern();

    logger.debug("pattern: {}", pattern);
    logger.debug("localized pattern: {}", localPattern);

    Date date;//w  w  w . ja v  a  2s  . c  o m

    try {
        date = new SimpleDateFormat(pattern).parse(string);
    } catch (ParseException ex) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "invalid date", pattern);
        throw new ConverterException(msg);
    }

    if (date.after(new Date())) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, rb.getString("birthdate_invalid"),
                rb.getString("birthdate_valid_future"));
        throw new ConverterException(msg);
    }

    Calendar c = Calendar.getInstance();
    c.set(1850, 1, 1);

    if (date.before(c.getTime())) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, rb.getString("birthdate_invalid"),
                rb.getString("birthdate_valid_past"));
        throw new ConverterException(msg);
    }

    return date;
}