下面列出了java.time.format.DateTimeParseException#getMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nullable
@Override
public T parse(@Nullable String value) throws ParseException {
if (StringUtils.isBlank(value)) {
return null;
}
DateTimeFormatter formatter;
if (formatPattern != null) {
formatter = DateTimeFormatter.ofPattern(formatPattern);
} else {
formatter = getDateTimeFormatter();
}
try {
return formatter.parse(value.trim(), newInstance());
} catch (DateTimeParseException ex) {
throw new ParseException(ex.getMessage(), 0);
}
}
@Nullable
@Override
public T parse(@Nullable String value, Locale locale) throws ParseException {
if (StringUtils.isBlank(value)) {
return null;
}
FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale);
if (formatStrings == null) {
return parse(value);
}
DateTimeFormatter formatter = getDateTimeFormatter(formatStrings, locale);
try {
return formatter.parse(value.trim(), newInstance());
} catch (DateTimeParseException ex) {
throw new ParseException(ex.getMessage(), 0);
}
}
/**
* Convert a Instant to the Zulu String format.
* See the <a href="http://developer.novell.com/documentation/ndslib/schm_enu/data/sdk5701.html">eDirectory Time attribute syntax definition</a> for more details.
*
* @param instant The Date to be converted
* @return A string formatted such as "199412161032Z".
*/
public static String convertInstantToZulu( final Instant instant )
{
if ( instant == null )
{
throw new NullPointerException();
}
try
{
return EDIR_TIMESTAMP_FORMATTER.format( instant.atZone( ZoneOffset.UTC ) );
}
catch ( DateTimeParseException e )
{
throw new IllegalArgumentException( "unable to format zulu time-string: " + e.getMessage() );
}
}
/**
* Convert the commonly used eDirectory zulu time string to java Date object.
* See the <a href="http://developer.novell.com/documentation/ndslib/schm_enu/data/sdk5701.html">eDirectory Time attribute syntax definition</a> for more details.
*
* @param input a date string in the format of "yyyyMMddHHmmss'Z'", for example "19941216103200Z"
* @return A Date object representing the string date
* @throws IllegalArgumentException if dateString is incorrectly formatted
*/
public static Instant convertZuluToInstant( final String input )
{
if ( input == null )
{
throw new NullPointerException();
}
try
{
final LocalDateTime localDateTime = LocalDateTime.parse( input, EDIR_TIMESTAMP_FORMATTER );
final ZonedDateTime zonedDateTime = localDateTime.atZone( ZoneOffset.UTC );
return Instant.from( zonedDateTime );
}
catch ( DateTimeParseException e )
{
throw new IllegalArgumentException( "unable to parse zulu time-string: " + e.getMessage() );
}
}
public static Date parseUtcTimeyyyyMMddhhmmss(String utcTime) {
String coreUtcTime = utcTime;
if (StringUtil.isNotBlank(utcTime)) {
char ch = utcTime.charAt(utcTime.length() - 1);
if (ch == 'z' || ch == 'Z') {
coreUtcTime = utcTime.substring(0, utcTime.length() - 1);
}
}
if (coreUtcTime == null || coreUtcTime.length() != 14) {
throw new IllegalArgumentException("invalid utcTime '" + utcTime + "'");
}
try {
LocalDateTime localDate = LocalDateTime.parse(coreUtcTime, SDF1);
Instant instant = localDate.atZone(ZONE_UTC).toInstant();
return Date.from(instant);
} catch (DateTimeParseException ex) {
throw new IllegalArgumentException("invalid utcTime '" + utcTime + "': " + ex.getMessage());
}
}
public static Date parseUtcTimeyyyyMMdd(String utcTime) {
String coreUtcTime = utcTime;
if (StringUtil.isNotBlank(utcTime)) {
char ch = utcTime.charAt(utcTime.length() - 1);
if (ch == 'z' || ch == 'Z') {
coreUtcTime = utcTime.substring(0, utcTime.length() - 1);
}
}
if (coreUtcTime == null || coreUtcTime.length() != 8) {
throw new IllegalArgumentException("invalid utcTime '" + utcTime + "'");
}
try {
LocalDateTime localDate = LocalDateTime.parse(coreUtcTime + "000000", SDF1);
Instant instant = localDate.atZone(ZONE_UTC).toInstant();
return Date.from(instant);
} catch (DateTimeParseException ex) {
throw new IllegalArgumentException("invalid utcTime '" + utcTime + "': " + ex.getMessage());
}
}
static long parseTimestamp(String timestamp) {
try {
return Long.parseLong(timestamp);
} catch (NumberFormatException e) {
TemporalAccessor dt;
try {
dt = TIMESTAMP_PARSER.parseBest(
timestamp, OffsetDateTime::from, LocalDateTime::from, LocalDate::from);
} catch (DateTimeParseException e1) {
throw new IllegalArgumentException(e1.getMessage());
}
if (dt instanceof LocalDateTime) {
LocalDateTime localDateTime = LocalDateTime.from(dt);
return localDateTime.toInstant(UTC).toEpochMilli();
} else if (dt instanceof LocalDate) {
LocalDate localDate = LocalDate.from(dt);
return localDate.atStartOfDay(UTC).toInstant().toEpochMilli();
}
OffsetDateTime offsetDateTime = OffsetDateTime.from(dt);
return offsetDateTime.toInstant().toEpochMilli();
}
}
static long parseTimestampIgnoreTimeZone(String timestamp) {
try {
return Long.parseLong(timestamp);
} catch (NumberFormatException e) {
TemporalAccessor dt;
try {
dt = TIMESTAMP_PARSER.parseBest(
timestamp, LocalDateTime::from, LocalDate::from);
} catch (DateTimeParseException e1) {
throw new IllegalArgumentException(e1.getMessage());
}
if (dt instanceof LocalDate) {
LocalDate localDate = LocalDate.from(dt);
return localDate.atStartOfDay(UTC).toInstant().toEpochMilli();
}
LocalDateTime localDateTime = LocalDateTime.from(dt);
return localDateTime.toInstant(UTC).toEpochMilli();
}
}
@Override
public String validateString(final String s) {
String err = null;
try {
LocalDate.parse(s);
} catch (final DateTimeParseException ex) {
err = ex.getMessage();
}
return err;
}
@SuppressWarnings("unchecked")
@Override
public boolean load(E object, Object value, PostUpdateProcessingStore<E> postupdateprocessingstore) {
DataObjectField<?, E> field = object.payload.lookupSimpleFieldOnName(name);
if (field == null)
throw new RuntimeException("field " + name + " could not be looked-up on " + object.getName());
if (!(field instanceof DateDataObjectField))
throw new RuntimeException("Expected field " + name
+ " would be of type DateDataObjectField but in reality, it is " + field.getClass().toString());
DateDataObjectField<E> datefield = (DateDataObjectField<E>) object.payload.lookupSimpleFieldOnName(name);
Date olddate = datefield.getValue();
try {
Date newdate = FlatFileLoader.parseDate(value,
"Field '" + field.getName() + "- with format " + formatasstring, timeedit, format);
if (FlatFileLoader.isTheSame(olddate, newdate)) {
return false;
} else {
logger.info(" *** dates are different " + olddate + " " + newdate);
datefield.setValue(newdate);
return true;
}
} catch (DateTimeParseException e) {
throw new RuntimeException("data is supposed to be date of format " + formatasstring + " for field '"
+ this.name + "' but received the following error when parsing '" + value + "'. Exception "
+ e.getMessage());
}
}
@Override
public Date parse(final String value) throws ParseException {
try {
return formatter == null || value == null || value.isEmpty() ? null : Date.from(Instant.from(LocalDateTime.parse(value, formatter)));
} catch (DateTimeParseException e) {
throw new ParseException(e.getMessage(), e.getErrorIndex());
}
}
private static ZonedDateTime tryParseDateHour(String dateTime) {
try {
try {
return ZonedDateTime.parse(dateTime, DATE_HOUR_FORMAT);
} catch (DateTimeParseException ignore) {
return LocalDate.parse(dateTime, DATE_HOUR_FORMAT).atStartOfDay(UTC);
}
} catch (DateTimeParseException e) {
throw new IllegalArgumentException(
"Cannot parse time parameter " + dateTime + " - " + e.getMessage(),
e);
}
}
public static TemporalAmount parseDurationValue(String text) {
try {
if(text.startsWith("PT")) {
return Duration.parse(text);
} else {
return Period.parse(text);
}
} catch (DateTimeParseException e) {
throw new IllegalArgumentException(e.getMessage() + ":" + text);
}
}
public static TemporalAccessor parseDateTimeValue(String text) {
try {
return ISO_8601_DATE_TIME.parseBest(text, OffsetDateTime::from, LocalDateTime::from);
} catch (DateTimeParseException e) {
try {
//Not parseable as a standard public object from datetime. We do not implement our own yet (we could!)
//so fallback to the Parsed object. The Parsed object is package-private, so cannot be added as a reference
//to the parseBest query, unfortunately.
return ISO_8601_DATE_TIME.parse(text);
} catch (DateTimeParseException e1) {
throw new IllegalArgumentException(e1.getMessage() + ":" + text);
}
}
}
public static TemporalAccessor parseTimeValue(String text) {
try {
return ISO_8601_TIME.parseBest(text, OffsetTime::from, LocalTime::from);
} catch (DateTimeParseException e) {
try {
//Not parseable as a standard public object from datetime. We do not implement our own yet (we could!)
//so fallback to the Parsed object. The Parsed object is package-private, so cannot be added as a reference
//to the parseBest query, unfortunately.
return ISO_8601_TIME.parse(text);
} catch (DateTimeParseException e1) {
throw new IllegalArgumentException(e1.getMessage() + ":" + text);
}
}
}
public static Temporal parseDateValue(String text) {
try {
return (Temporal) ISO_8601_DATE.parseBest(text, LocalDate::from, YearMonth::from, Year::from);
} catch (DateTimeParseException e) {
throw new IllegalArgumentException(e.getMessage() + ":" + text);
}
}