简体   繁体   中英

Spring - LocalDateTime - how to return just the hours and minutes instead of dd-mm-yyyy hh-MM-ss-sss

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

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)

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM