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;
}
Aggregations