Java Utililty Methods DateTimeFormatter

List of utility methods to do DateTimeFormatter

Description

The list of methods to do DateTimeFormatter are organized into topic(s).

Method

StringgetTime(String format)
get Time
LocalDateTime dt = LocalDateTime.now();
return String.format(format, dt.getYear(), dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(),
        dt.getMinute(), dt.getSecond());
StringgetTimeFromEpochMilli(String value, boolean shortFormat, String errorText)
Converts a number of milliseconds since 1970 to an regular date
try {
    return Instant.ofEpochMilli(Long.parseLong(value)).atZone(ZoneId.systemDefault())
            .format(DateTimeFormatter.ofPattern(shortFormat ? "dd.MM.yy HH:mm" : "dd. MMMM yyyy, HH:mm"));
} catch (NumberFormatException | DateTimeException ex) {
    return errorText;
StringgetTimeStamp(final String dateTimeFormatPattern)
Return time stamp formatted corresponding to input dateTimeFormatPattern pattern.
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateTimeFormatPattern));
PathgetTimestampedPath(Path baseFile, TemporalAccessor time, DateTimeFormatter formatter)
Creates a timestamped path from the given base file.
return getTimestampedPath(baseFile, formatter.format(time));
Instantparse(DateTimeFormatter formatter, String string)
Parses a string into an Instant .
Instant now = null;
int year = 0;
int month = 0;
int dayOfMonth = 0;
int hour = 0;
int minute = 0;
int second = 0;
int nanoOfSecond = 0;
...
ZonedDateTimeparse(final DateTimeFormatter formatter, final String value, final ZoneId zoneId)
parse
final TemporalAccessor temporalAccessor = formatter.parseBest(value, ZonedDateTime::from,
        LocalDateTime::from, LocalDate::from);
if (temporalAccessor instanceof ZonedDateTime) {
    return ((ZonedDateTime) temporalAccessor).withZoneSameInstant(zoneId);
if (temporalAccessor instanceof LocalDateTime) {
    return ((LocalDateTime) temporalAccessor).atZone(zoneId);
return ((LocalDate) temporalAccessor).atStartOfDay(zoneId);
Instantparse(String _date, String _format)
Get an Instant from string at local zone
DateTimeFormatter format = DateTimeFormatter.ofPattern(_format);
try {
    return LocalDateTime.parse(_date, format).toInstant(localZoneOffset);
} catch (DateTimeParseException e) {
    try {
        return LocalDate.parse(_date, format).atStartOfDay().toInstant(localZoneOffset);
    } catch (DateTimeParseException e2) {
        throw e2;
...
LocalDateTimeparse(String format, String datetime)
parse
return parse(DateTimeFormatter.ofPattern(format), datetime);
StringparseAndFormat(String date, DateTimeFormatter fromFormatter, DateTimeFormatter toFormatter)
parse And Format
LocalDateTime localDateTime = LocalDateTime.parse(date, fromFormatter);
return localDateTime.format(toFormatter);
LocalDateTimeparseDateTime(String dateTimeStr, String format)
Parse date time based on format (e.g yyyy/MM/dd HH:mm:ss)
LocalDateTime date = null;
try {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
    date = LocalDateTime.parse(dateTimeStr, formatter);
} catch (DateTimeParseException exc) {
    System.out.printf("%s is not parsable!%n %s", dateTimeStr, exc);
return date;
...