0

I use spring boot as the basic framework, I define a Long format field named startTime, but when the input value is a string like "2020-04-21 12:00:00", the framework converts "2020-04-21 12:00:00" to a timestamp like 253652458665256 automatically. here I don't need it to be converted. I need the original value. because I will check the startTime value whether it satisfies my rule.

i tried to set something in application.properties.like spring.jackson.deserialization.adjust-dates-to-context-time-zone=false. but it does not work.

problems describedenter image description here in the image below

supplementary instruction: 1. The reason for using a Long type to store the time of date is UTC timestamp ignoring timezone. because it refers to timezone in my project, so for the convenient of handling date, UTC timestamp is the first choice.

  • 3
    Why have you used Long to store a date value? – Asmat K Apr 21 '20 at 07:23
  • 1
    How do you java stores a String into a Long? Or rather Text into a number? That is obviously not going to happen. \ – M. Deinum Apr 21 '20 at 07:58
  • 1
    Take a look at this [answer](https://stackoverflow.com/questions/7487460/converting-long-to-date-in-java-returns-1970#20646345) and do not use `Long` type for a `Date`. – Michał Ziober Apr 21 '20 at 07:58
  • It sounds like you want to store and use the "long" value of [java.util.Date.getTime()](https://docs.oracle.com/javase/7/docs/api/java/util/Date.html). If so, why not just 1) accept the input as a string (e.g. `"2020-04-21 12:00:00"`), then 2) `SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");`, and finally 3) `long startTime = sdf.parseDate(s).getTime();` – FoggyDay Apr 22 '20 at 02:20

1 Answers1

0

There are some properties for date-format

spring.jackson.date-format=
spring.jackson.time-zone=
spring.jackson.write-dates-as-timestamps =false

And use Date to store the value

private Date startTime;
private Date endTime;

if possible (> Java 7)

private LocalDateTime startTime;
private LocalDateTime endTime;

java-8-date-time-intro

dezhi.shen
  • 84
  • 4
  • 1
    `Date` is obsolete now, if possible (> `Java 7`) try to use type from `Java Time` package. See [Introduction to the Java 8 Date/Time API](https://www.baeldung.com/java-8-date-time-intro) – Michał Ziober Apr 21 '20 at 08:00
  • Further, if you're in a pre-java time world, I'm inclined to suggest using joda-time. – hd1 Apr 22 '20 at 02:09