String 類
/**
* String 簡介 final修飾的類
*/
public class Demo{
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);//內存地址的比較//true
int len = s1.length();
for (int i = 0; i < len; i++) {
//charAt(int index)獲取指定位置的字符
System.out.println(s1.charAt(i));//a//b//c
}
String s3 = "http://www.baidu.com/images/logdgre/dgo.gif";
if (s3 != null && s3.length() > 0) {
//從字符串中截取文件名(后部分數據)
String fileName = s3.substring(s3.lastIndexOf("/") + 1);
System.out.println(fileName);//dgo.gif
//截取前部分數據
String prefix = s3.substring(0, s3.indexOf(":"));
System.out.println(prefix);//http
}
//比較
String s4 = "acdaaa";
String s5 = "cd";
//進行內容比較
System.out.println(s4.equalsIgnoreCase(s5));//false
//判斷 是否包含子字符串
System.out.println(s4.contains(s5));//true
//將字符串轉為字節數組
String name = "張三";
//默認編碼為unicode, 可選的有utf-8,gbk,gb2312,iso8859-1
byte[] bytes;
try {
bytes = name.getBytes("gb2312");//gbk和gb2312編碼中,每一個漢字點2個字節
System.out.println(bytes.length);//4
//注意: 編碼和解碼必須保持一致
System.out.println(new String(bytes, "gb2312"));//張三
//基本數據類型轉成字符串
String s6 = String.valueOf(100);
s6 = String.valueOf(78.85);
//查看字符串的hashCode()
System.out.println("張三".hashCode() + "," + name.hashCode());
//比較字符中,如果兩個字符串完全相等,則返回0
System.out.println("abd".compareTo("abddddddd"));//-6
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
StringBuilder 類
public class Demo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("?bdsalbdgs");
//構造器模式,鏈式編程風格
sb.append("dasd").append("&psd=")
.append("123123").append("&phone=")
.append("3424341423542");
String url = sb.toString();
System.out.println(url);
//在頭部插入" ?offset=1&"
sb.insert(url.indexOf("?") + 1, "offset=1&");
System.out.println(sb.toString());
}
}
自動拆裝箱
/**
* 自動拆裝箱
*/
public class Demo {
public static void main(String[] args) {
String ss = "19.5";
Double ssD = Double.parseDouble(ss);
//int ssI=Integer.parseInt(ss); //報異常,不能將帶有小數的字符串轉成整數
System.out.println(ssD.toString());//19.5
System.out.println(Integer.toString(34, 10));//轉成指定進制(轉成十進制)//34
Integer a = 100; //自動裝箱: 先創建Integer,然后將100存入到Integer創建的內存中
int sum = a + 10; //自動拆箱: 先調用a.intValue()將其值取出,與10進行計算,將結果賦于sum
System.out.println(sum);//110
Integer a1 = 100;
Integer a2 = 100;
//java默認情況下,會在常量池中創建-128~127常量對象
System.out.println(a1 == a2);//true
////////////////////////////////
Integer a5 = 234;
Integer a6 = 234;
//java默認情況下,會在常量池中創建-128~127常量對象
System.out.println(a5 == a6);//false
////////////////////////////////
Integer a3 = 0;
System.out.println(a1 == (a2 + a3));//true
System.out.println(a1.hashCode() + "," + a2.hashCode());//true
//注:整型數值的hashCode()是其本身,除Long之外(有可能不相同)
Long l1 = new Long(1234567800L);
System.out.println(l1.hashCode());//1234567800
}
}
Calendar類
public class Demo {
public static void main(String[] args) {
// 獲取日歷對象
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);// 獲取年
int month = cal.get(Calendar.MONTH); // 獲取月份
int day = cal.get(Calendar.DAY_OF_MONTH);// 日
int hour = cal.get(Calendar.HOUR_OF_DAY); // 24小時
int minute = cal.get(Calendar.MINUTE);// 分鐘
int second = cal.get(Calendar.SECOND);
int week=cal.get(Calendar.DAY_OF_WEEK);//星期(1~7,1:周日,7:周六)
System.out.println(String.format("%d-%d-%d %d:%d:%d", year, month + 1,
day, hour, minute, second));
System.out.println("================================");
String[] wName={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
System.out.println(wName[week-1]+","+week);
System.out.println("================================");
cal.set(Calendar.MONTH, 12-1);//12月份
//查看每一個月中的天數
for(int i=1;i<=cal.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
cal.set(Calendar.DAY_OF_MONTH, i);
week=cal.get(Calendar.DAY_OF_WEEK);
System.out.println(wName[week-1]+","+week+","+(i)+"號");
}
}
}
Date類
/**
* Date類
*/
public class Demo {
public static void main(String[] args) {
Date date = new Date();//獲取當前的時間
System.out.println(date.toString());
long time = date.getTime();
Date d2 = new Date(time);
System.out.println(d2.toString()); //獲取Long類型的時期值
//使用定義好的日期格式化工具類
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);//獲取日期的格式化對象
DateFormat dtf = DateFormat.getDateTimeInstance();//獲取日期和時間格式化對象
System.out.println(df.format(date));
System.out.println(dtf.format(date));
try {
Date d3 = df.parse("17-07-19");
System.out.println(d3.toString());
} catch (ParseException e) {
e.printStackTrace();
}
//自定義格式化日期或時間
//注:月 MM,分鐘:mm,ss: 秒,SSS:毫秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
System.out.println(sdf.format(date));
}
}
運行結果

補充日期相減
public class Demo {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = sdf.parse("2016-09-08 10:10:10");
Date d2 = sdf.parse("2016-09-15 00:00:00");
System.out.println(daysBetween(d2, d1));
//System.out.println(daysBetween("2012-09-08 10:10:10","2012-09-15 00:00:00"));
}
public static int daysBetween(Date smdate, Date bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 字符串的日期格式的計算
*/
public static int daysBetween(String smdate, String bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
}
補充
int和Integer有什么區別?
Integer 是int 的包裝類.
原始類型: boolean,char,byte,short,int,long,float,double
包裝類型:Boolean,Character,Byte,Short,Integer,Long,Float,Double
-
注意
public static void main(String[] args) { Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150; System.out.println(f1 == f2);//true System.out.println(f3 == f4);//false }
如果整型字面量的值在-128到127之間,那么不會new新的Integer對象,而是直接引用常量池中的Integer對象,所以上面的面試題中f1==f2的結果是true,而f3==f4的結果是false。
Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?
Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。四舍五入的原理是在參數上加0.5然后進行下取整。
是否可以繼承String類?
String 類是final類,不可以被繼承。
String和StringBuilder、StringBuffer的區別?
String和StringBuffer/StringBuilder,它們可以儲存和操作字符串。其中String是只讀字符串,也就意味著String引用的字符串內容是不能被改變的。而StringBuffer/StringBuilder類表示的字符串對象可以直接進行修改。StringBuilder是Java 5中引入的,它和StringBuffer的方法完全相同,區別在于它是在單線程環境下使用的,因為它的所有方面都沒有被synchronized修飾,因此它的效率也比StringBuffer要高。
String s = new String("xyz");創建了幾個字符串對象?
兩個對象,一個是靜態區的"xyz",一個是用new創建在堆上的對象。
怎樣將GB2312編碼的字符串轉換為ISO-8859-1編碼的字符串?
String s1 = "你好";
String s2 = new String(s1.getBytes("GB2312"), "ISO-8859-1");