Not without a hack: replace org.drools.core.util.DateUtils by the
modified code given below. Specify drools.dateformat as a list of date
formats separated by a newline, e.g.:
System.setProperty( "drools.dateformat", "dd-MMM-yyyy
HH:mm\ndd-MMM-yyyy" );
Make sure that a more comprehensive format is first, they are tried in
the given order, and a longer String is accepted for a shorter format.
The very first format is used for printing (whenever Drools feels like
doing so). On error, you may see a message such as
Invalid date input format: [1-5-1985] it should follow one of:
[dd-MMM-yyyy HH:mm] or [dd-MMM-yyyy]
And the default format is dd-MMM-yyyy - NOT dd-mmm-yyyy as "Expert"
would have it.
-W
package org.drools.core.util;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.drools.type.DateFormats;
public class DateUtils {
private static final long serialVersionUID = 510l;
private static final String DEFAULT_FORMAT_MASK = "dd-MMM-yyyy";
private static final String DATE_FORMAT_MASK = getDateFormatMask();
private static final String DEFAULT_COUNTRY = Locale.getDefault()
.getCountry();
private static final String DEFINE_COUNTRY = getDefaultContry();
private static final String DEFAULT_LANGUAGE = Locale.getDefault()
.getLanguage();
private static final String DEFINE_LANGUAGE = getDefaultLanguage();
private static ThreadLocal<SimpleDateFormat[]> df = new
ThreadLocal<SimpleDateFormat[]>() {
protected SimpleDateFormat[] initialValue() {
DateFormatSymbols dateSymbol = new DateFormatSymbols(new Locale(
DEFINE_LANGUAGE, DEFINE_COUNTRY));
String[] masks = getDateFormatMask().split( "\\n" );
SimpleDateFormat[] dateFormats = new SimpleDateFormat[masks.length];
for( int i = 0; i < masks.length; i++ ){
dateFormats[i] = new SimpleDateFormat(masks[i], dateSymbol);
}
return dateFormats;
};
};
private static String getDefaultLanguage() {
String fmt = System.getProperty("drools.defaultlanguage");
if (fmt == null) {
fmt = DEFAULT_LANGUAGE;
}
return fmt;
}
private static String getDefaultContry() {
String fmt = System.getProperty("drools.defaultcountry");
if (fmt == null) {
fmt = DEFAULT_COUNTRY;
}
return fmt;
}
/** Use the simple date formatter to read the date from a string */
public static Date parseDate(final String input, DateFormats dateFormats) {
for( SimpleDateFormat dateFormat: df.get() ){
try {
Date date = dateFormat.parse( input );
return date;
} catch (final ParseException e) {
}
}
String valids = DATE_FORMAT_MASK.replace( "\n", "] or [" );
throw new IllegalArgumentException("Invalid date input format: ["
+ input + "] it should follow one of: [" + valids +
"]");
}
/** Use the simple date formatter to convert the Date into a String */
public static String format(final Date input) {
return df.get()[0].format( input );
}
/** Converts the right hand side date as appropriate */
public static Date getRightDate(final Object object2, DateFormats
dateFormats) {
if (object2 == null) {
return null;
}
if (object2 instanceof String) {
return parseDate((String) object2, dateFormats);
} else if (object2 instanceof Date) {
return (Date) object2;
} else {
throw new IllegalArgumentException("Unable to convert "
+ object2.getClass() + " to a Date.");
}
}
/** Check for the system property override, if it exists */
public static String getDateFormatMask() {
String fmt = System.getProperty("drools.dateformat");
if (fmt == null) {
fmt = DEFAULT_FORMAT_MASK;
}
return fmt;
}
}
On 31/01/2013, richie <haoruiqian(a)gmail.com> wrote:
Hi,
The default date format in drools is 'dd-MMM-yyyy', but in the definition
of
rule attribute date-effective, it says it contain a date and time
definition, so if I set date-effective to "30-Jan-2013 08:00", then the
time
set in date-effective will be ignored, so I changed the date format to
'dd-MMM-yyyy HH:mm', now the effective date works correctly, but then we
got
problem here, if user input a date like "30-Jan-2013" the drools will
failed
to execute, so must force user to input a date like this "30-Jan-2013
00:00", this is not user friendly and the string "00:00" is meaningless.
What I want to know is, if it's possible to keep this two formats both?
Thanks.
--
View this message in context:
http://drools.46999.n3.nabble.com/Is-it-posibble-to-keep-two-date-formats...
Sent from the Drools: User forum mailing list archive at
Nabble.com.
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users