/** * Converts a string date in the given format to a long timestamp * (in Unix format: milliseconds since January 1, 1970) with special * milliseconds handling. * * @param date The date to convert * @param format The date format to parse the string date * @param separator The separator for milliseconds * @return The date in milliseconds */ public static long getTime(String date, String format, String separator) { Instant timestamp = parseDate(date, format, separator); if (timestamp == null) { return -1; } else { return timestamp.getMillis(); } }
/** * Converts a string date in the given format to a long timestamp * (in Unix format: milliseconds since January 1, 1970) with special * milliseconds handling. * * @param date The date to convert * @param format The date format to parse the string date * @param separator The separator for milliseconds * @return The date in milliseconds */ public static long getTime(String date, String format, String separator) { Instant timestamp = parseDate(date, format, separator); if (timestamp == null) { return -1; } else { return timestamp.getMillis(); } }
/** * Formats the given date as an ISO 8601 date. * * @param date The date to format as ISO 8601 * @param format The date format to parse the string date * @param lenient Whether or not to leniently parse the date. * @param separator The separator for milliseconds */ public static String formatDate(String date, String format, boolean lenient, String separator) { if (date == null) return null; Instant timestamp = parseDate(date, format, separator); if (timestamp == null) { return null; } final DateTimeFormatter isoformat; if ((timestamp.getMillis() % 1000) != 0) { isoformat = ISO8601_FORMATTER_MS; } else { isoformat = ISO8601_FORMATTER; } return isoformat.print(timestamp); }
/** * Formats the given date as an ISO 8601 date. * * @param date The date to format as ISO 8601 * @param format The date format to parse the string date * @param lenient Whether or not to leniently parse the date. * @param separator The separator for milliseconds * @return an ISO 8601 formatted timestamp */ public static String formatDate(String date, String format, boolean lenient, String separator) { if (date == null) return null; Instant timestamp = parseDate(date, format, separator); if (timestamp == null) { return null; } final DateTimeFormatter isoformat; if ((timestamp.getMillis() % 1000) != 0) { isoformat = ISO8601_FORMATTER_MS; } else { isoformat = ISO8601_FORMATTER; } return isoformat.print(timestamp); }