Untitled
unknown
plain_text
a year ago
1.4 kB
2
Indexable
Never
public static Long stringToTimestamp(String timeText, String pattern, String timezone) throws ParseException { Long timestamp; try { DateTimeFormatter dfs = new DateTimeFormatterBuilder() .appendOptional(DateTimeFormatter.ofPattern(pattern)) .toFormatter(Locale.US); timestamp = LocalDateTime.parse(timeText, dfs) .atZone(ZoneId.of(timezone)) .toEpochSecond(); } catch (Exception e) { try { DateTimeFormatter dfs = new DateTimeFormatterBuilder() .appendOptional(DateTimeFormatter.ofPattern(pattern)) .optionalStart() .append(DateTimeFormatter.ofPattern(pattern)) .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .optionalEnd() .toFormatter(Locale.US); timestamp = LocalDateTime.parse(timeText, dfs) .atZone(ZoneId.of(timezone)) .toEpochSecond(); } catch (Exception e1) { return null; } } return timestamp; }