• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

java.util.Date to int

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Im new in java and im having trouble transforming the date into integer i tried using long but gives me more then I want, I only want to convert to int yyyymmdd

Thank you for the help.

Heres my code

Is it possible to convert to int only the year month and day in the same int?
 
Marshal
Posts: 71070
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use Date. It is a really badly designed class. Look in the Java™ Tutorials to find what has superseded it.
No, you can't convert a Date to an int. Date stores a number representing the milliseconds since 1st January 1970 (midnight GMT). If you try to use an int, that has a capacity of <2³¹ and you will suffer an overflow error if the time difference is more than about 3½ weeks. You can get a long out of a Date, which can accommodate something like ±292,000,000 years. Or you can use this method and get the same result subject to the precision of your computer's clock.
 
Diogo Malta
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I converted to long but in the database i want to return a int(8)
and the value that the variable dataFinal is returning ins on this day 1603883824076
is this the date in nanoseconds?
 
Saloon Keeper
Posts: 6711
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which date is that supposed to be?

Wouldn't int(8) be the same as a long?
 
Diogo Malta
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Which date is that supposed to be?

Wouldn't int(8) be the same as a long?




Its today the input was 2020/10/28

but the out put is more than 8 characters.
 
Tim Moores
Saloon Keeper
Posts: 6711
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An int doesn't have characters, it has bytes. "long" is Java's name for 8-byte ints: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
 
Tim Moores
Saloon Keeper
Posts: 6711
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Diogo Malta wrote:1603883824076


If that is today, then the representation is likely milliseconds since the epoch (meaning, since 1970/1/1).
 
Rancher
Posts: 4743
50
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're sticking this in a database then just leave it as a Date and put that in the database.
If the database is (for whatever reason) using an 8 character field for its Date, then format it into an 8 character String (yyyyMMdd).
 
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Storing a date as an int(8) is a very poor design choice, in my opinion. Most databases support a Date/Time stamp datatype so why wouldn't you just use that instead of int(8)? Whatever it is you think you're saving, it's just not worth the time and effort and pain you're going to experience.

See the replies here to see what kind of problems an int(8) representation of Date will give you: https://dba.stackexchange.com/questions/15645/storing-date-as-integer-numeric-what-are-the-advantages
 
Diogo Malta
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Storing a date as an int(8) is a very poor design choice, in my opinion. Most databases support a Date/Time stamp datatype so why wouldn't you just use that instead of int(8)? Whatever it is you think you're saving, it's just not worth the time and effort and pain you're going to experience.

See the replies here to see what kind of problems an int(8) representation of Date will give you: https://dba.stackexchange.com/questions/15645/storing-date-as-integer-numeric-what-are-the-advantages



I agree with you, but the thing is that i cant change the database, and they have the field of the date in int(8) thats the problem
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Diogo Malta wrote:I agree with you, but the thing is that i cant change the database, and they have the field of the date in int(8) thats the problem


That's not a very good reason to just keep going in that direction. The more code you add that supports this poor design choice, the messier things get for you. It's best to change course now. I would add a new column to the database with the proper type, then do a batch conversion from int(8) to DATE/TIME. Of course, you're going to have to transition any existing code now that deals with the int(8) to use DateTime instead.

The longer you go on with your current scheme, the more pain you're creating for yourself in the long term.
 
Campbell Ritchie
Marshal
Posts: 71070
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope the 8 in int(8) means bytes rather than bits
 
Dave Tolls
Rancher
Posts: 4743
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Digits.
 
Saloon Keeper
Posts: 22790
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the sound of it, the database has a binary value in it that needs to be broken into pieces before you can even begin to build any sort of date/calendar object out of it.

That is, if "i" is the "date", then you need year = i / 1000, month = i % 1000 / 100 and day = i % 100. Then you can construct a civilized calendar object out of it.

Going the other way is easier, since you can create a DateFormat template of yyyyMMdd, format the date object into a String, then convert the String to an int.

But really, a better result in the long run is a native database timestamp type object. In a world where commerce spans multiple time zones, a brute-force date can be a problem. If I order something from London at 8PM local time, it's already beginning a new day there.

It's also worth checking to see if the current database schema's "date" is truly an int or if it's something like a FoxPro value where it's actually a string containing nothing but digits.
 
Marshal
Posts: 26131
77
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say you're new to Java. Is this code you're working on also a new Java project which uses an existing database? I ask because if it isn't new then there surely must already be code in the project which accesses that date-as-integer field. Look for that code.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic