String to int java.lang.NumberFormatException -
i have java program convert string
int
, rang of string 190520141618013381
(above rang of int
) when convert int
java.lang.numberformatexception:
thrown
stringbuffer stringbuffer = new stringbuffer(); stringbuffer.append(format.format(date)); stringbuffer.append(demandcount); int test_int = integer.parseint(stringbuffer.tostring()); // exception has been fixed putting //long abc_i = long.parselong(abc); log.info("test_int: "+test_int);
my question compiler should throw numberoutofrangexception
(if exception available in api) instead java.lang.numberformatexception:
, format of number(190520141618013381
) right.
the string 190520141618013381
outside range of int
doesn't match accepted format of int
because long.
the compiler doesn't throw error, thrown @ runtime.
i believe correct comply documentation method.
btw don't use stringbuffer, replaced stringbuilder ten years ago.
imho storing date integer isn't idea in general.
a more efficient unique id contains time in millis this.
private static final atomiclong ts_counter = new atomiclong(); public static long nexttimestamp() { long time = system.currenttimemillis() * 1000; long curr = ts_counter.get(); if (curr < time && ts_counter.compareandset(curr, time)) return time; return ts_counter.incrementandget(); }
this have time-in-millis * 1000 plus unique id. works fine if average less 1 million ids per second.
Comments
Post a Comment