S
S
sanek_gyy2019-10-13 15:27:16
Java
sanek_gyy, 2019-10-13 15:27:16

How to customize Timestamp format?

There is an essence

import java.sql.Timestamp;

@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Car {

    @Id
    @GeneratedValue
    @JsonIgnore
    private Long id;

    @NotNull(message = "car number can't be null")
    @Pattern(regexp="[A-Z0-9\\- ]{4,16}", message = "invalid car number format")
    private String carNumber;

    private Timestamp timestamp;
}

Before saving which I put down a timestamp
@Override
    public Car saveCar(Car car) {
        car.setTimestamp(new Timestamp(System.currentTimeMillis()));
        return carRepository.save(car);
    }

How to make date format output without milliseconds?
5da31782b8966422164446.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2019-10-13
@sanek_gyy

Specify date format for json:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "[email protected]:mm:ss")
private Date createdDate;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question