how to convert long to int
hi fellas. i was wondering i you could help me. in postgre db i have a field order_time. its data type is int4. there are a couple of records, where order_time is 1536936174. so i have a couple of questions:
1. i was wondering how to set this field value, when im doing insert in my Java code?
2. why the date is stored in a column that has datatype of
int4
? why not date
or datetime
?
3. in java code i see that my entity is private Long orderTime
, so does that mean theres a mistake in the code and i need to use Long in the database too? or do i use int in my Java code?
thanks alot11 Replies
⌛
This post has been reserved for your question.
Hey @bambyzas! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
1: Just set the number using
System.currentTimeMillis() / 1000
(and prey they set your server up correctly with timezones etc)
2: Because someone who designed the database didn't know what they were doing, or thought that integers would be "more performant" or some other random reason that isn't really valid
3: use an Integer in your code (or int if it's always mandatory)thanks. now its more clear whats going on. can u elaborate on the ssecond point? why its bad to use unix timestamp?
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Usually it was used when databases didn't have proper support for decent date and time formats
But now timestamp exists in postgres for example why still use it?
Using the correct type limits you or gives you the possibility to do things that make sense for that type
PostgreSQL Documentation
8.5. Date/Time Types
8.5. Date/Time Types # 8.5.1. Date/Time Input 8.5.2. Date/Time Output 8.5.3. Time Zones 8.5.4. Interval Input 8.5.5. Interval Output PostgreSQL supports …
See all the things you can do?
That only make sense on timestamps/dates times etc
oh, i didnt know that
my first reaction was also "there are dedicated datatypes for dates, why use int"
It falls under the general typed programming thing. If you have the type, use it
Like you wouldn't use a string for a uuid
If you use a timestamp in postgres you can use an instant in your Java code
So even more type safety etc etc
thank you
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
💤
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.