I am using the calendar library to save the date from my app. I am using sqlite3 and can only save ints and strings to my db. Calendar converts to a long that when converts back shows the correct date. However to save it in my db I have to convert it to an int. When I convert the int back to a long and use setTimeInMillis(), my date changes. I know that when you covert from long to an int you lose precision. Is there a work around? I was told that I have to use the calendar library for what I am doing. Below is the code that is giving me problems, any ideas. Already did a google search and did not have any luck
long timing=0;
timing =dateAndTime.getTimeInMillis();
testDate.setTimeInMillis(timing);
int dates= (int) timing;
timing=dates;
testDate.setTimeInMillis(timing);
It’s not that you’re losing precision. It’s more that a long holds more bytes than an integer so you’re gonna lose whatever data the int can’t hold. You’re better off converting the long to a String and storing it as that (
Long.toString(l)). When you want to get it out of the database just parse the String into a long (Long.parseLong(s)).