1.String
import org.junit.Test;
/*
* String:代表不可变的字符序列;底层使用char[]存放
* String是final的
*/
public class testString {
@Test
public void test5(){
String str1 = "520";
int i = Integer.parseInt(str1);
System.out.println(i);
double temp = 520.52052603d;
float temp2 = 520.5205667f;//520.52057
String doubleString = String.valueOf(temp);
String floatString = String.valueOf(temp2);
System.out.println("double:" + doubleString);
System.out.println("float:" + floatString);
}
@Test
public void test4(){
/*
* public String[] split(String str) : 按照字符串str对当前字符串进行拆分,整体返回值是String[]
*/
String str1 = "a*dd*f-d*ad*156";
//因为+、*、|、\等符号在正则表达示中有相应的不同意义,所以在使用时要进行转义处理。
String str2 = "\\*";//或者 str2 = "[*]";
String[] str = str1.split(str2);
for(int i = 0;i < str.length;i++){
System.out.println(str[i]);
}
System.out.println("------");
String[] str3 = str1.split("-");
for(String strTemp:str3){
System.out.println(strTemp);
}
}
@Test
public void test3(){
/*
* public String trim() : 去除当前字符串中首尾的空格,若有多个,就去除多个
*/
String str1 = " a bcc ";
String str2 = str1.trim();
System.out.println("---" + str1 + "--");
System.out.println("---" + str2 + "--");
/*
* public String concat(String str) : 将当前字符串与str连接
*/
String str3 = "我是一个小伙子";
String str4 = "、长得也还可以。";
System.out.println(str3.concat(str4));
}
@Test
public void test2(){
/*
* public String substring(int start,int end) : 返回从start开始到end结束的左闭右开的字符串
*
*/
String str1 = "我是一个 热爱学习、并且长得也还不错的小伙子。";
String str2 = str1.substring(0, str1.length());
String str3 = "0123456789";
String str4 = str3.substring(0,7);
System.out.println(str2);
System.out.println(str4);
}
@Test
public void test1(){
String str1 = "JavaEE";
String str2 = "JavaEE";
String str3 = new String("JavaEE");
String str4 = "JavaEE" + "Android";
String str5 = "Android";
String str6 = str1 + str5;
str5 = str5 + "Handoop";
String str7 = str6.intern();
String str8 = "JavaEEAndroid";
System.out.println(str1 == str2);//true
System.out.println(str1 == str3);//false
System.out.println(str1.equals(str3));//true
System.out.println(str4 == str6);//false
System.out.println(str4 == str7);//true
System.out.println(str4 == str8);
Person p1 = new Person("AA");
Person p2 = new Person("AA");
System.out.println("^_^"+ (p1.name == p2.name));
}
}
class Person{
String name;
public Person(String name) {
this.name = name;
}
}
2.StringBuffer,StringBuilder
java.lang.StringBuffer:可变字符序列,可以对字符序列内容进行增删
* >线程安全
* >效率相对低
* java.lang.StringBuilder:可变字符序列
* >线程不安全
* >效率相对高
*
*
*
* 添加:append();
* 插入:public StringBuffer insert(int index,String str)
* 反转:public StringBuffer reverse()
* 删除:StringBuffer delete(int startIndex,int endIndex) 左闭右开
* 修改:public void setCharAt(int index,char ch)
* 取代:StringBuffer replace(int startIndex,int endIndex,String str) 左闭右开
* 剪切:public String substring(int start,int end) 左闭右开
public static void main(String[] args) {
/*
* 对比StringBuffer StringBuild String 三者在添加上的效率
* StringBuild > StringBuffer > String
*/
String text = "";
long startTime = 0L;
long endTime = 0L;
StringBuffer buffer = new StringBuffer("");
StringBuilder builder = new StringBuilder("");
startTime = System.currentTimeMillis();
for(int i = 1;i <= 50000;i++)
buffer.append(i);
endTime = System.currentTimeMillis();
System.out.println("StringBuffer 所用的时间是:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for(int i = 1;i <= 50000;i++)
builder.append(i);
endTime = System.currentTimeMillis();
System.out.println("StringBuilder 所用的时间是:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for(int i = 1;i <= 50000;i++)
text += i + "";
endTime = System.currentTimeMillis();
System.out.println("String 所用的时间是:" + (endTime - startTime));
}
3.Date
/*
* 与时间相关的类
* 1.System 类下的 currentTimeMillis();
* 2.Date类:java.util.Date 以及其子类 java.sql.Date
* 3.SimpleDateFormat类
* 4.Calendar 类
*/
public class TestDate {
//日历类
@Test
public void test4(){
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
c.add(Calendar.DAY_OF_MONTH, -1);
day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
c.set(Calendar.DAY_OF_MONTH, 25);
Date d = c.getTime();
System.out.println(d);
}
/*
* "三天打渔,两天晒网" "1990-01-01" XXXX-XX-XX 打渔?晒网?
*/
public int getTime(String date1,String date2) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = sdf.parse(date1);
Date d2 = sdf.parse(date2);
long millTime = d2.getTime() - d1.getTime();
long time = millTime/1000/3600/24;
//double time = Math.ceil((double)millTime/1000/3600/24);
//System.out.println("^_^"+millTime/1000/3600/24);
return (int) time+1;//time=0表示第一天,所以这里应该加一才对
}
@Test
public void test3() throws ParseException{
String date1 = "1990-01-01";
String date2 = "1990-01-0";
for(int i = 1;i<=9;i++){
//date2 = date2 + i+"";
//System.out.println(date2+i);
int temp = getTime(date1, date2+i);
if(temp % 5 == 0 || temp % 5 == 4){
System.out.println(i+":晒网");
}else{
System.out.println(i+":打渔");
}
}
}
/*
* 利于国际化:日期-->文本
* 解析:文本-->日期
*/
@Test
public void test2() throws ParseException{
//默认格式化
SimpleDateFormat sdf = new SimpleDateFormat();
String date = sdf.format(new Date());
System.out.println(date); //17-11-18 下午11:45
//指定格式化
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date2 = sdf2.format(new Date());
System.out.println(date2);//2017-11-18 23:52:28
sdf2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
date2 = sdf2.format(new Date());
System.out.println(date2);//星期六, 18 十一月 2017 23:57:07 +0800
//解析
Date date3 = sdf.parse("17-11-18 下午11:45");
/*
* 传入的参数必须和当前对象的构造器一致
* 错误写法:
* sdf.parse("EEE, d MMM yyyy HH:mm:ss Z")
*/
System.out.println(date3);//Sat Nov 18 23:45:00 CST 2017
date3 = sdf2.parse("星期六, 18 十一月 2017 23:57:07 +0800");
System.out.println(date3);//Sat Nov 18 23:57:07 CST 2017
}
@Test
public void test1(){
Date d1 = new Date();
System.out.println(d1);//Sat Nov 18 23:36:50 CST 2017
System.out.println(d1.getTime());//1511019410086
Date d2 = new Date(1511019410086L);
System.out.println(d2);//Sat Nov 18 23:36:50 CST 2017
//SQL中的日期类,传入一个long类型的值,返回一个日期,如:2017-11-18
java.sql.Date d3 = new java.sql.Date(1511019374333L);
System.out.println(d3);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}