ZonedDateTime parse issue

When I pass this to my endpoint:
{
"product": "gas",
"date": "2024-03-11T10:00:00+03:00",
"value": 1738
}
{
"product": "gas",
"date": "2024-03-11T10:00:00+03:00",
"value": 1738
}
for this entity:
@Entity
@Table(name="reading")
public class Reading {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String product;

@ManyToOne
private User user;

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssXXX")
private ZonedDateTime date;

private BigDecimal value;
@Entity
@Table(name="reading")
public class Reading {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String product;

@ManyToOne
private User user;

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssXXX")
private ZonedDateTime date;

private BigDecimal value;
in my database as a result I get: 2024-03-11 09:00:00.000 for date. It should be 2024-03-11 09:00:00+03:00 - isn't this covered by @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssXXX")
13 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @the goat! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan7mo ago
Databases don't store time zone info. How could the database possibly contain something like +03:00?
the goat
the goatOP7mo ago
should I store it as a string or I should ignore the timezone in the database
Kyo-chan
Kyo-chan7mo ago
You should configure the database to use UTC as the time zone so that you know what time zone it uses. Then you should expect everything you read in it to be in UTC
the goat
the goatOP7mo ago
can I do that in Spring I am sorry if my question is stupid I am just a bit lost
Kyo-chan
Kyo-chan7mo ago
No, Spring doesn't operate databases. Database operators do
tjoener
tjoener7mo ago
That's not a ZonedDateTime That's an OffsetDateTime ZonedDateTimes have timezones, this is an offset (+03:00)
Kyo-chan
Kyo-chan7mo ago
Offsets are representable as time zones, so essentially OffsetDateTimes are a subset of ZonedDateTimes
tjoener
tjoener7mo ago
I can have an offset that says +3:45 What timezone would that be? You can turn a timezone and a local datetime into an offset yes Not the other way around though
Kyo-chan
Kyo-chan7mo ago
Dude, try first, tell others they're wrong after. It will be time zone +3:45 as far as ZonedDateTime is concerned
tjoener
tjoener7mo ago
No description
tjoener
tjoener7mo ago
OK that's gross So it seems an OffsetDateTime has a LocalDateTime and a ZoneOffset, and a ZonedDateTime has the same things but ALSO a ZoneId
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server