Android: How to get/convert long to int?
android, int, java, long-integer
Solution
long to int:
public static int safeLongToInt(long l) {
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
throw new IllegalArgumentException
(l + " cannot be cast to int without changing its value.");
}
return (int) l;
}
long to String:
public static String longToString(long l) {
return String.valueOf(l);
}
Problem
``` Calendar c = Calendar.getInstance(); long diff = c.getTimeInMillis() - DateSaved.getTimeInMillis(); //result in millis long daysDiff = (diff / (24 * 60 * 60 * 1000)); ``` Everything works fine, when i print diff (integer number of days)... The problem starts here: ``` int daysDiffINT = (int) daysDiff; int daysRemaining = (sumaINT/dailyINT) - daysDiffINT; ``` i need to proceed my long diff with some int values. This above doesn't work.The console gives me an error: "Invalid int" I searched a little bit, there are thousands of converting String to Int etc. But how can I get int from long? Or String from long? It's driving me crazy :( // BIG Thanks for all of you - you've learned me how to make my own app :)