Search in sources :

Example 1 with TimeZoneAwareDatatype

use of com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype in project cuba by cuba-platform.

the class DatatypeFormatter method parseDateTime.

/**
 * Parse Date (date and time) using {@code dateTimeFormat} string specified in the main message pack.
 * <p>Takes into account time zone if it is set for the current user session.</p>
 * @return Date value or null if a blank string is provided
 */
@Nullable
public Date parseDateTime(String str) throws ParseException {
    TimeZone timeZone = uss.getUserSession().getTimeZone();
    Datatype<Date> datatype = Datatypes.getNN(Date.class);
    if (datatype instanceof TimeZoneAwareDatatype) {
        return (Date) ((TimeZoneAwareDatatype) datatype).parse(str, uss.getLocale(), timeZone);
    }
    return datatype.parse(str, uss.getLocale());
}
Also used : TimeZone(java.util.TimeZone) TimeZoneAwareDatatype(com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype) Date(java.util.Date) Nullable(javax.annotation.Nullable)

Example 2 with TimeZoneAwareDatatype

use of com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype in project cuba by cuba-platform.

the class ListEditorHelper method getValueCaption.

public static String getValueCaption(Object v, ListEditor.ItemType itemType, TimeZone timeZone) {
    if (v == null)
        return null;
    switch(itemType) {
        case ENTITY:
            if (v instanceof Instance)
                return ((Instance) v).getInstanceName();
            else
                return v.toString();
        case STRING:
            return (String) v;
        case DATE:
            return Datatypes.getNN(java.sql.Date.class).format(v);
        case DATETIME:
            UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
            UserSession userSession = userSessionSource.getUserSession();
            if (timeZone != null) {
                return ((TimeZoneAwareDatatype) Datatypes.getNN(Date.class)).format(v, userSession.getLocale(), timeZone);
            } else {
                return Datatypes.getNN(Date.class).format(v, userSession.getLocale());
            }
        case INTEGER:
            return Datatypes.getNN(Integer.class).format(v);
        case LONG:
            return Datatypes.getNN(Long.class).format(v);
        case BIGDECIMAL:
            return Datatypes.getNN(BigDecimal.class).format(v);
        case DOUBLE:
            return Datatypes.getNN(Double.class).format(v);
        case ENUM:
            return AppBeans.get(Messages.class).getMessage((Enum) v);
        case UUID:
            return Datatypes.getNN(java.util.UUID.class).format(v);
        default:
            throw new IllegalStateException("Unknown item type");
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) TimeZoneAwareDatatype(com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype) Messages(com.haulmont.cuba.core.global.Messages) Instance(com.haulmont.chile.core.model.Instance) Date(java.util.Date) BigDecimal(java.math.BigDecimal) UserSession(com.haulmont.cuba.security.global.UserSession)

Example 3 with TimeZoneAwareDatatype

use of com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype in project cuba by cuba-platform.

the class DatatypeFormatter method formatDateTime.

/**
 * Format Date (date and time) using {@code dateTimeFormat} string specified in the main message pack.
 * <p>Takes into account time zone if it is set for the current user session.</p>
 * @return string representation or empty string if the value is null
 */
public String formatDateTime(@Nullable Date value) {
    TimeZone timeZone = uss.getUserSession().getTimeZone();
    Datatype<Date> datatype = Datatypes.getNN(Date.class);
    if (datatype instanceof TimeZoneAwareDatatype) {
        return ((TimeZoneAwareDatatype) datatype).format(value, uss.getLocale(), timeZone);
    }
    return datatype.format(value, uss.getLocale());
}
Also used : TimeZone(java.util.TimeZone) TimeZoneAwareDatatype(com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype) Date(java.util.Date)

Aggregations

TimeZoneAwareDatatype (com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype)3 Date (java.util.Date)3 TimeZone (java.util.TimeZone)2 Instance (com.haulmont.chile.core.model.Instance)1 Messages (com.haulmont.cuba.core.global.Messages)1 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)1 UserSession (com.haulmont.cuba.security.global.UserSession)1 BigDecimal (java.math.BigDecimal)1 Nullable (javax.annotation.Nullable)1