天天看点

java的常见类型转换

如何将long类型转换为string

//方法一:

当我们要把String转化为Integer时,一定要对String进行非空判断,否则很可能报空指针异常。

String str = "...";

Integer i = null;

if(str!=null){

i = Integer.valueOf(str);

}

获取一个变量的类型

        <code>   </code><code>public</code> <code>static</code> <code>void</code> <code>main(String[] args) {</code>

<code>        </code><code>Test test = </code><code>new</code> <code>Test();</code>

<code>        </code><code>System.out.println(test.getClass().getSimpleName());</code>

<code>    </code><code>}</code>

<code>}</code>

1.Integer转换成int的方法

Integer i;  int k = i.intValue(); 即Integer.intValue();

2.int转换成Integer

int i;

Integer it = new Integer(i);

3.String转换成int的方法

String str = "10";   Integer it = new Interger(str);  

int i = it.intValue();  

即:int i = Integer.intValue(string);

4.int转换成String

(1)String s = String.valueOf(i);

(2)String s = Ingeger.toString(i);

(3)String s = "" + i;

5.String转换成Integer

String str = "10"

Integer it = Integer.valueOf(str);

6.Integer转换成String

Integer it;

String str = it.toString();

7.String转换成BigDecimal

BigDecimal bd = new BigDecimal(str);

8.日期

Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH)+1; int day = calendar.get(Calendar.DATE);

//获取今天的日期字符串 String today = Java.text.DateFormat.getDateInstance().format(new java.util.Date()); //获取今天的日期 new java.sql.Date(System.currentTimeMillis());