List of utility methods to do DateTimeFormatter
String | getTime(String format) get Time LocalDateTime dt = LocalDateTime.now();
return String.format(format, dt.getYear(), dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(),
dt.getMinute(), dt.getSecond());
|
String | getTimeFromEpochMilli(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; |
String | getTimeStamp(final String dateTimeFormatPattern) Return time stamp formatted corresponding to input dateTimeFormatPattern pattern. return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateTimeFormatPattern));
|
Path | getTimestampedPath(Path baseFile, TemporalAccessor time, DateTimeFormatter formatter) Creates a timestamped path from the given base file. return getTimestampedPath(baseFile, formatter.format(time));
|
Instant | parse(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; ... |
ZonedDateTime | parse(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); |
Instant | parse(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; ... |
LocalDateTime | parse(String format, String datetime) parse return parse(DateTimeFormatter.ofPattern(format), datetime);
|
String | parseAndFormat(String date, DateTimeFormatter fromFormatter, DateTimeFormatter toFormatter) parse And Format LocalDateTime localDateTime = LocalDateTime.parse(date, fromFormatter);
return localDateTime.format(toFormatter);
|
LocalDateTime | parseDateTime(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; ... |