Search in sources :

Example 16 with DateTime

use of lucee.runtime.type.dt.DateTime in project Lucee by lucee.

the class DateCaster method toDateAdvanced.

/**
 * converts a String to a DateTime Object (Advanced but slower), returns null if invalid string
 * @param str String to convert
 * @param convertingType one of the following values:
 * - CONVERTING_TYPE_NONE: number are not converted at all
 * - CONVERTING_TYPE_YEAR: integers are handled as years
 * - CONVERTING_TYPE_OFFSET: numbers are handled as offset from 1899-12-30 00:00:00 UTC
 * @param timeZone
 * @param defaultValue
 * @return Date Time Object
 */
public static DateTime toDateAdvanced(String str, short convertingType, TimeZone timeZone, DateTime defaultValue) {
    str = str.trim();
    if (StringUtil.isEmpty(str))
        return defaultValue;
    // every format has digits
    if (!hasDigits(str))
        return defaultValue;
    timeZone = ThreadLocalPageContext.getTimeZone(timeZone);
    DateTime dt = toDateSimple(str, convertingType, true, timeZone, defaultValue);
    if (dt == null) {
        final DateFormat[] formats = FormatUtil.getCFMLFormats(timeZone, true);
        synchronized (formats) {
            Date d;
            ParsePosition pp = new ParsePosition(0);
            for (int i = 0; i < formats.length; i++) {
                // try {
                pp.setErrorIndex(-1);
                pp.setIndex(0);
                d = formats[i].parse(str, pp);
                if (pp.getIndex() == 0 || d == null || pp.getIndex() < str.length())
                    continue;
                dt = new DateTimeImpl(d.getTime(), false);
                return dt;
            }
        }
        dt = toDateTime(Locale.US, str, timeZone, defaultValue, false);
    }
    return dt;
}
Also used : DateFormat(java.text.DateFormat) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) DateTime(lucee.runtime.type.dt.DateTime) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Aggregations

DateTime (lucee.runtime.type.dt.DateTime)16 DateTimeImpl (lucee.runtime.type.dt.DateTimeImpl)5 Locale (java.util.Locale)4 Date (java.util.Date)3 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 TimeZone (java.util.TimeZone)2 CasterException (lucee.runtime.exp.CasterException)2 ExpressionException (lucee.runtime.exp.ExpressionException)2 PageException (lucee.runtime.exp.PageException)2 ObjectWrap (lucee.runtime.type.ObjectWrap)2 TimeImpl (lucee.runtime.type.dt.TimeImpl)2 TimeSpan (lucee.runtime.type.dt.TimeSpan)2 Node (org.w3c.dom.Node)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1