Converting Object to int or String in java

                                         Converting Object to int or String in java

 

 

Object obj = getObject();
if(obj instanceof Integer)
  int value = (Integer)obj;

   

The method of converting String to int type:
1. Integer.parseInt([String])
2.Integer.valueOf([String]).intValue();
3.Integer.decode([String]): Decode String to Integer. Accepts decimal, hexadecimal and octal numbers given by the following syntax
E.g:
int a=Integer.decode("0144"); //The result of octal conversion is 100
int b=Integer.decode("123");//The decimal conversion result is 123
int c=Integer.decode("0x123");//The hexadecimal conversion result is 291
Note: Integer.decode([String]) plus minus sign can also be converted, but the string cannot have spaces. Otherwise report NumberFormatException
Note: The method of converting a string to Double, Float, Long is similar.

Convert int to String type method:
1.String s = String.valueOf(i);
2.String s = Integer.toString(i);
3. String s = "" + i;
seldom used:
1.Integer.toBinaryString(int i) : Returns the string representation of an integer argument as a binary (base 2) unsigned integer.
2. Integer.toHexString(int i) : Returns the string representation of an integer argument as a hexadecimal (base 16) unsigned integer.
3. Integer.toOctalString(int i): Returns the string representation of an integer argument as an octal (base 8) unsigned integer.
Note: The methods of converting Double, Float, and Long into strings are similar.
The object type is converted to the int type:
1. If the object is generated by byte, short, int, or char type, it is ok to assign it directly without conversion.
2. If the object is generated from a string type, first convert the object to a String type, and then convert the String type to an int type.
E.g.
String myInt="123";
Object os=myInt;
int b=Integer.parseInt((String)os);//You can also os.toString()
3. If the object is generated by float, double, or long type, the idea is the same as above, first convert the object to the corresponding data type, and then convert it to the int type.

Convert object type to String type:
String title=String.valueOf(obj[2]);
String content=String.valueOf(obj[3]);
The object type is converted to the Date type:
SimpleDateFormat can convert String to Date and Date to String.
.parse(String)
.format(Date)
which is:
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
String indate=sdf.format(date);
Date indate=sdf.parse(String);	
				
If you use SpinnerDateModel.
You can try to cast your Object directly to Date
Date date = (Date) object;

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326024402&siteId=291194637