Object in Java converted to int or string type method

Source: Internet
Author: User

Reprint: http://www.cnblogs.com/1020182600HENG/p/6137206.html

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

1 method of converting string to int type: 1.  Integer.parseint ([string]) 2.integer.valueof ([string]). Intvalue (); 3.integer.decode ([String]): Decodes the String to an Integer. Accepts decimal, hexadecimal, and octal digits given by the following syntax, for example: int A=integer.decode ("0144");  The octal conversion result is B=integer.decode ("123"), or the decimal conversion result is 123 int c=integer.decode ("0x123"), and the//16 binary conversion result is 291 Note: Integer.decode ([string]) plus minus sign can also be converted, but the string cannot have spaces. Otherwise reported NumberFormatException exception Note: string to Double, Float, Long method is similar.

convert int to String type method: 1.String s = string.valueof (i); 2.String s = integer.tostring (i); 3. String s = "" + I; Infrequently used: 1.integer.tobinarystring (int i): Returns a string representation of an integer argument in binary (radix 2) unsigned integer form. 2.integer.tohexstring (int i): Returns the string representation of an integer parameter as a 16-based (radix 16) unsigned integer. 3.integer.tooctalstring (int i): Returns the string representation of an integer parameter as a eight-based (radix 8) unsigned integer. Note: Double, Float, Long turn into a string in a similar way.

The object type is converted to the int type: 1. If object is generated by the Byte,short,int,char type, then no direct assignment to the conversion is OK. 2. If object is generated as a string type, first convert the object to a string type, and then convert the string type to the int type. For example. String myint= "123"; Object Os=myint; int B=integer.parseint (String) OS;//can also os.tostring () 3. If object is generated as a float,double,long type, the idea is the same as above, Convert the object to the appropriate data type and then to the int type.


The object type is converted 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 either convert a string to date or date to string.
. Parse (String)
. Format (Date)
That
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 this object directly to date.
Date date = (date) object;

Object in Java converted to int or string type method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.