Java資料類型中String、Integer、int相互間的轉換

來源:互聯網
上載者:User

            Java資料類型中String、Integer、int相互間的轉換

 

 這幾個轉換老把我弄得暈暈的, 索性找了篇文章,一看倒也還蠻清晰的。by the way, 文章來自於:
                                                                                                       http://blog.sina.com.cn/cnxiaotao

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

int i;

(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());






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.