
org.ow2.bonita.util.DateUtil Maven / Gradle / Ivy
package org.ow2.bonita.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public abstract class DateUtil {
private DateUtil() {
}
public static final SimpleDateFormat DATE_FORMAT_1 = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss/SSS");
public static final SimpleDateFormat DATE_FORMAT_2 = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss");
// ISO-8601 requires the T between date and time.
public static final SimpleDateFormat ISO_8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
public static Date parseDate(final String formattedDate) {
Date result;
try {
result = DateUtil.DATE_FORMAT_1.parse(formattedDate);
} catch (final ParseException e1) {
try {
result = DateUtil.DATE_FORMAT_2.parse(formattedDate);
} catch (final ParseException e2) {
try {
result = DateUtil.ISO_8601_FORMAT.parse(formattedDate);
} catch (final ParseException e3) {
throw new IllegalArgumentException(formattedDate + " is not a formatted date."
+ " Supported formats are " + DateUtil.DATE_FORMAT_2.toPattern() + ", "
+ DateUtil.DATE_FORMAT_1.toPattern() + " and (ISO-8601) " + DateUtil.ISO_8601_FORMAT.toPattern());
}
}
}
return result;
}
public static String format(final Date date) {
return DateUtil.ISO_8601_FORMAT.format(date);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy