Please tell me what to do in this case: the json model comes where there are some fields with date or time recorded in different formats, I don’t fully understand what I need to convert them into models so that I can write to the database and produce what or manipulation. For example, the standard date format of the form: yyyy-MM-dd hh: mm: ss converting to Date can be written to the database in the form of a long UTC timestamp and when extracting the date, it can be converted to local time, but what to do, for example, with yyyy-MM -dd or just with hh: mm: ss?

    2 answers 2

    It all depends on the task that you are facing. I suspect that the date format does not change randomly for the same field. Therefore, for each of them should use the appropriate format.

    For the date (full or not) use the timestamp, for the time I would choose integer=hours*60*60+minutes*60+seconds

    while the incomplete date is saved as yyyy-mm-dd 00:00

       public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm"); public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yy"); public static final DateTimeFormatter DATE_FORMATTER_2 = DateTimeFormatter.ofPattern("dd-MM-yyyy"); public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"); public static final DateTimeFormatter DATE_TIME_FORMATTER_2 = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm"); public static final DateTimeFormatter DATE_TIME_FORMATTER_3 = DateTimeFormatter.ofPattern("HH:mm dd.MM.yyyy"); public static String timeFormat(final TemporalAccessor time) { return TIME_FORMATTER.format(time); } public static String dateFormat(final TemporalAccessor dateTime) { return DATE_FORMATTER_2.format(dateTime); } public static String dateFormat(final Instant instant) { final ZonedDateTime dateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); return dateFormat(dateTime); } public static String dateTimeFormat(final TemporalAccessor dateTime) { return DATE_TIME_FORMATTER_3.format(dateTime); } public static String dateTimeFormat(final Instant instant) { final ZonedDateTime dateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); return dateTimeFormat(dateTime); } 

      Here is the standard formatter, use for health