java常用類

來源:互聯網
上載者:User

標籤:格式化   tostring   基本   字串   eee   gettime   數組   日曆   date類   

1. String類:不可變的字元序列(如:String str = "atguigu"; str += "javaEE")
1.關注於String常用的方法!
2.String類與基礎資料型別 (Elementary Data Type)、封裝類;與字元數組、位元組數組;
* 1.字串 與基礎資料型別 (Elementary Data Type)、封裝類之間轉換
* ①字串 --->基礎資料型別 (Elementary Data Type)、封裝類:調用相應的封裝類的parseXxx(String str);
* ①基礎資料型別 (Elementary Data Type)、封裝類--->字串:調用字串的重載的valueOf()方法
*
* 2.字串與位元組數組間的轉換
* ①字串---->位元組數組:調用字串的getBytes()
* ②位元組數組---->字串:調用字串的構造器
*
* 3.字串與字元數組間的轉換
* ①字串---->字元數組:調用字串的toCharArray();
* ②字元數組---->字串:調用字串的構造器
4.String與StringBuffer的轉換
①String --->StringBuffer:使用StringBuffer的構造器:new StringBuffer(String str);
②StringBuffer----->String:使用StringBuffer的toString()方法

StringBuffer類:可變的字元序列
StringBuilder類:可變的字元序列,jdk5.0新加入的,效率更高,線程不安全。
常用的方法:添加:append(...) 刪除 delete(int startIndex, int endIndex) 修改:setCharAt(int n ,char ch) 查詢:charAt(int index)
插入:insert(int index, String str) 反轉reverse() 長度:length()

註:String類的不可變性:

 

 

2.時間、日期類:
2.1System類 currentTimeMillis():返回目前時間的long型值。此long值是從1970年1月1日0點0分00秒開始到當前的毫秒數。
此方法常用來計算時間差。
2.2 Date類:java.util.Date
1. Date d = new Date();//返回目前時間的Date:Mon May 12 15:17:01 CST 2014
Date d1 = new Date(15231512541241L);//返回形參處此long型值對應的日期
//getTime():返回當前日期對應的long型值。 toString()
2.3SimpleDateFormat:java.text.SimpleDateFormat
格式化 :日期--->文本 使用SimpleDateFormat的format()方法
解析:文本--->日期 使用SimpleDateFormat的parse()方法
//1.格式化1
SimpleDateFormat sdf = new SimpleDateFormat();
String date = sdf.format(new Date());
System.out.println(date);//14-5-12 下午3:24
//2.格式化2
SimpleDateFormat sdf1 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
date = sdf1.format(new Date());
System.out.println(date);//星期一, 12 五月 2014 15:29:16 +0800

//3.解析:
Date date1 = sdf.parse("14-5-12 下午3:24");
System.out.println(date1);

date1 = sdf1.parse("星期一, 12 五月 2014 15:29:16 +0800");
//date1 = sdf1.parse("14-5-12 下午3:24");
System.out.println(date1);
2.4 Calendar:日曆類
2.4.1擷取執行個體:Calendar c = Calendar.getInstance();
2.4.2 get()/set()/add()/date getTime()/setTime()

3.Math類

4.BigInteger BigDecimal類

 

java常用類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.