1

I have a column called starttime in my 'workout' table in my DB, I am trying to use LocalDateTime to save the start time to that column. In my workout class, I have entered the following:

@JsonFormat(pattern="HH:mm")
@Column(name = "starttime")
private LocalDateTime startTime; 

I am then trying to save a testWorkout in my dataloader as follows:

Workout testWorkout = new Workout(LocalDateTime.now(), 88.00);
workoutRepository.save(testWorkout);

when I run my application and check in postgresql / postico, I see the start time as: 2022-02-01 12:53:26.488

How do I get it to just log hours and minutes?

I am fairly new to this so any help would be appreciated.

Thanks

João Dias
  • 16,277
  • 6
  • 33
  • 45
Raza88
  • 21
  • 2
  • @Column(name = "local_time", columnDefinition = "TIME") private LocalTime localTime; https://stackoverflow.com/questions/40368585/how-to-keep-time-in-spring-data-jpa – Rafał Sokalski Feb 01 '22 at 13:12
  • you can use this syntax: localDateTime.format(DateTimeFormatter.ofPattern("HH:mm")) – Issa Khodadadi Feb 01 '22 at 13:20
  • LocalDateTime having method getHour() and getMinute() if it fits in your requirement plz use it. – Raushan Kumar Feb 01 '22 at 13:23
  • thank you all for your suggestions. I got it working with the following: ``` @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="EEE MMM dd HH:mm:ss Z yyyy") @JsonProperty("starttime") @Column(name = "starttime", columnDefinition = "TIME") private LocalTime startTime;``` – Raza88 Feb 01 '22 at 14:17

2 Answers2

2

There is a special class for this: LocalTime. Also to see proper way to do so look at this question (although there they show the example with ZonedDateTime)

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • thats awesome! thank you for that. I got it working with the following code: ``` @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="EEE MMM dd HH:mm:ss Z yyyy") @JsonProperty("starttime") @Column(name = "starttime", columnDefinition = "TIME") private LocalTime startTime;``` – Raza88 Feb 01 '22 at 14:17
0

https://www.postgresql.org/docs/current/datatype-datetime.html

Table 8.14. Date/Time Output Styles
Style Specification     Description     Example
ISO       ISO 8601, SQL standard      1997-12-17 07:37:16-08
SQL       traditional style           12/17/1997 07:37:16.00 PST
Postgres    original style        Wed Dec 17 07:37:16 1997 PST
German       regional style               17.12.1997 07:37:16.00 PST

from this chart,which means, database query output can not return time as "HH-MM". So you need to format it to char/text type. 24 hour type:

select to_char(localtime(0),'HH24:MI'); 

12 hour type

 select to_char(localtime(0),'HH12:MI');
jian
  • 4,119
  • 1
  • 17
  • 32