Question

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 :)

Was it helpful?

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top