Java Code Examples for java.time.format.DateTimeParseException#getMessage()

The following examples show how to use java.time.format.DateTimeParseException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractTemporalDatatype.java    From cuba with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 2
Source File: AbstractTemporalDatatype.java    From cuba with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 3
Source File: EdirEntries.java    From ldapchai with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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() );
    }
}
 
Example 4
Source File: EdirEntries.java    From ldapchai with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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() );
    }
}
 
Example 5
Source File: DateUtil.java    From xipki with Apache License 2.0 6 votes vote down vote up
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());
  }
}
 
Example 6
Source File: DateUtil.java    From xipki with Apache License 2.0 6 votes vote down vote up
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());
  }
}
 
Example 7
Source File: TimestampType.java    From crate with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 8
Source File: TimestampType.java    From crate with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 9
Source File: LocalDateParameterType.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public String validateString(final String s) {
    String err = null;
    try {
        LocalDate.parse(s);
    } catch (final DateTimeParseException ex) {
        err = ex.getMessage();
    }

    return err;
}
 
Example 10
Source File: DateDataObjectFieldFlatFileLoader.java    From Open-Lowcode with Eclipse Public License 2.0 5 votes vote down vote up
@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());
	}

}
 
Example 11
Source File: DateParser.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@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());
    }
}
 
Example 12
Source File: ParameterUtil.java    From styx with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 13
Source File: TemporalConstraintParser.java    From archie with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 14
Source File: TemporalConstraintParser.java    From archie with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 15
Source File: TemporalConstraintParser.java    From archie with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 16
Source File: TemporalConstraintParser.java    From archie with Apache License 2.0 5 votes vote down vote up
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);
    }
}