JAVA常用类
一、String类
1.String的不可变性
package Test01;
import org.junit.Test;
public class StringTest1 {
@Test
public void test1(){
String s1="fan";
String s2="fan";
s1="FYX";
System.out.println (s1);
System.out.println (s2);
System.out.println ("**************");
String s3="fan123";
String s4="fan123";
s3+="1116";
System.out.println (s3);
System.out.println (s4);
System.out.println ("**************");
String s7 = s5.replace ("fan", "YYY");
System.out.println (s7);
System.out.println (s6);
System.out.println ("**************");
}
}
2.string 的实例化
@Test
public void test2(){
String s1="fan";
String s2="fan";
String s3=new String ("fan");
String s4=new String ("fan");
System.out.println (s1==s2);
System.out.println (s1==s3);
System.out.println (s3==s4);
System.out.println ("**********");
Person p1=new Person ("Tom",18);
Person p2=new Person ("Tom",20);
System.out.println (p1.name.equals (p2.name));
System.out.println (p1.name==p2.name);
}
3.string类不同拼接的对比
@Test
public void test3(){
String s1="Tom";
String s2="cat";
String s3="Tomcat";
String s4="Tom"+"cat";
String s5=s1+"cat";
String s6="Tom"+s2;
String s7=s1+s2;
System.out.println (s3==s4);
System.out.println (s3==s5);
System.out.println (s3==s7);
System.out.println (s5==s6);
System.out.println (s5==s7);
String s8 = s5.intern ();
System.out.println (s8==s3);
}
4.string类面试题
package Test01;
public class StringTest2 {
String str=new String ("good");
char[] ch={'t','e','s','t'};
public void change(String str,char ch[]){
str="test ok";
ch[0]='b';
}
public static void main(String[] args) {
StringTest2 stringTest2=new StringTest2 ();
stringTest2.change (stringTest2.str,stringTest2.ch);
System.out.println (stringTest2.ch);
System.out.println (stringTest2.str);
}
}
5.string类的常用方法
常用方法一:
package Test01;
import org.junit.Test;
public class StringTest3 {
@Test
public void test1(){
String s1="HelloWord";
System.out.println (s1.length ());
System.out.println (s1.charAt (5));
System.out.println (s1.isEmpty ());
System.out.println (s1.toLowerCase ());
System.out.println (s1.toUpperCase ());
System.out.println ("***************");
String s2=" Hello word ";
System.out.println ("——"+s2+"——");
System.out.println ("——"+s2.trim ()+"——");
System.out.println ("***************");
String s3="helloword";
System.out.println (s1.equals (s3));
System.out.println (s1.equalsIgnoreCase (s3));
System.out.println (s1.concat (" haha"));
System.out.println (s3.substring (5));
System.out.println (s3.substring (0,5));
}
}
常用方法二:
@Test
public void test2(){
String s1="HelloWord";
System.out.println (s1.endsWith ("ord"));
System.out.println (s1.startsWith ("He"));
System.out.println (s1.startsWith ("lo",3));
System.out.println (s1.contains ("ord"));
System.out.println (s1.indexOf ("lo"));
System.out.println (s1.indexOf ("lo",2));
System.out.println ("*************");
System.out.println (s1.lastIndexOf ("or"));
System.out.println (s1.lastIndexOf ("or",7));
}
常用方法三:
@Test
public void test3(){
String s1="易烊千玺超级无敌巨巨巨巨无霸帅";
System.out.println (s1.replace ("帅","棒"));
System.out.println (s1.replaceAll ("巨",""));
System.out.println (s1.replaceFirst ("易烊千玺","霸王花"));
System.out.println ("************************");
}
6.string类与包装类的转换
public class StringTest4 {
@Test
public void test(){
String s1="123";
int i = Integer.parseInt (s1);
System.out.println (i);
System.out.println ("*************");
String s = String.valueOf (i);
System.out.println (s);
String s2=""+i;
System.out.println (s2);
}
}
7.string类与char[]的转换
@Test
public void test2(){
String s1="fan1116";
char[] chars = s1.toCharArray ();
for(int i=0;i<chars.length;i++){
System.out.println (i);
}
char[] c={'F','Y','X'};
new String(c);
System.out.println (c);
}
8.string与byte[]之间的转换
@Test
public void test3() {
String s1="fan霸王花";
byte[] bytes = s1.getBytes ();
System.out.println (Arrays.toString (bytes));
System.out.println ("**************");
String s2=new String (bytes);
System.out.println (s2);
}
9.练习题:
10.StringBuffer和StringBuilder类
11.StringBuffer的常用方法
@Test
public void test1(){
StringBuffer s1=new StringBuffer ("易烊千玺超级无敌巨无霸帅!");
System.out.println (s1);
System.out.println ( s1.append ("哈哈哈哈"));;
System.out.println (s1.replace (12,18,"啊对对对"));;
System.out.println (s1.reverse ());
s1.reverse ();
System.out.println (s1.length ());
System.out.println ( s1.substring (0,12));
System.out.println (s1.charAt (11));
}
}
12.三者效率比较
13.System类中获取时间的方法
package Test03;
import org.junit.Test;
import java.util.Date;
public class TimeTest {
@Test
public void test2(){
Date date=new Date();
System.out.println (date.toString ());
System.out.println (date.getTime ());
Date date1=new Date (1638519995369L);
System.out.println (date1.toString ());
System.out.println (date1.getTime ());
}
@Test
public void test1(){
long time = System.currentTimeMillis ();
System.out.println (time );
}
}
二、时间类
1. System类中currentTimeMillis()
@Test
public void test1(){
long time = System.currentTimeMillis ();
System.out.println (time );
}
2.Date类
@Test
public void test2(){
Date date=new Date();
System.out.println (date.toString ());
System.out.println (date.getTime ());
Date date1=new Date (1638519995369L);
System.out.println (date1.toString ());
System.out.println (date1.getTime ());
}
3.SimpleDateFormat类
package other;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleTime {
@Test
public void testSimpleDate() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-DD hh:mm:ss");
Date date=new Date ();
String s = sdf.format (date );
System.out.println (s);
Date date2 = sdf.parse (s);
System.out.println (date2);
}
@Test
public void test() throws ParseException {
String birth="2019-11-16";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Date date = sdf.parse (birth);
java.sql.Date birthDate = new java.sql.Date (date.getTime ());
System.out.println (birthDate);
}
@Test
public void test2() throws ParseException {
String start="1990-01-01";
String end="2019-11-16";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Date date1 = sdf.parse (start);
Date date2 = sdf.parse (end);
long time =( (date2.getTime () - date1.getTime ()) / (1000 * 60 * 60 * 24) )+ 1;
long l = time % 5;
if(l==1||l==2||l==3){
System.out.println (end+"在打渔");
}else if(l==0||l==4){
System.out.println ("end+在晒网");
}
}
}
4.Calendar(日历)类
@Test
public void CalendarTest(){
Calendar calendar = Calendar.getInstance ();
int day = calendar.get (Calendar.DAY_OF_MONTH);
System.out.println (day);
calendar.set (DAY_OF_MONTH,22);
day = calendar.get (DAY_OF_MONTH);
System.out.println (day);
calendar.set (DAY_OF_MONTH,3);
day = calendar.get (DAY_OF_MONTH);
System.out.println (day);
Date date = calendar.getTime ();
System.out.println (date);
Date date1 = new Date ();
calendar.setTime (date1);
day= calendar.get (Calendar.DAY_OF_MONTH);
System.out.println (day);
}
5.LocalDateTime 的使用
@Test
public void LocalDateTime(){
LocalDateTime localDateTime = LocalDateTime.now ();
System.out.println (localDateTime);
LocalDateTime localDateTime1 = LocalDateTime.of (2019, 11, 16, 11, 28);
System.out.println (localDateTime);
System.out.println (localDateTime1);
System.out.println ("************************");
System.out.println (localDateTime.getMonthValue ());
System.out.println (localDateTime.getDayOfMonth ());
System.out.println ("************************");
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth (11);
System.out.println (localDateTime);
System.out.println (localDateTime2);
LocalDateTime localDateTime3 = localDateTime.withHour (5);
System.out.println (localDateTime);
System.out.println (localDateTime3);
System.out.println ("************************");
LocalDateTime localDateTime4 = localDateTime.plusDays (5);
System.out.println (localDateTime);
System.out.println (localDateTime4);
LocalDateTime localDateTime5 = localDateTime.minusMinutes (20);
System.out.println (localDateTime);
System.out.println (localDateTime5);
}
三、JAVA比较器
1.compare 接口
package Test03;
import org.junit.Test;
import java.util.Arrays;
public class JavaCom {
@Test
public void ComparableTest(){
String []arr={"DD","AA","FF","CC","BB","EE"};
Arrays.sort (arr);
System.out.println (Arrays.toString (arr));
}
@Test
public void SelfComarableTest(){
Goods []arr1=new Goods[5];
arr1[0]=new Goods (23,"Lenovo");
arr1[1]=new Goods (53,"dell");
arr1[2]=new Goods (32,"HuaWei");
arr1[3]=new Goods (12,"LuoJi");
arr1[4]=new Goods (32,"Mi");
Arrays.sort (arr1);
System.out.println (Arrays.toString (arr1));
}
}
package Test03;
public class Goods implements Comparable{
private int price;
String name;
public Goods(int price, String name) {
this.price = price;
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getName() {
return name;
}
public void setNeme(String name) {
this.name = name;
}
@Override
public String toString() {
return "Goods{" + "price=" + price + ", name='" + name + '\'' + '}';
}
@Override
public int compareTo(Object o) {
if(o instanceof Goods){
Goods goods=(Goods)o;
if(this.price>((Goods) o).price){
return 1;
}else if(this.price<((Goods) o).price){
return -1;
}else
return this.name.compareTo (goods.name);
}
throw new RuntimeException ("传入的数据类型不一致!");
}
}
2.Comparator
@Test
public void ComparatorTest(){
String []arr={"DD","AA","FF","CC","BB","EE"};
Arrays.sort (arr, new Comparator<String> () {
@Override
public int compare(String o1, String o2) {
if(o1 instanceof String && o2 instanceof String){
String s1=(String)o1;
String s2=(String)o2;
return -s1.compareTo (s2);
}
throw new RuntimeException ("输入的数据类型有错误!");
}
});
System.out.println (Arrays.toString (arr));
}
@Test
public void ComparatorTest2(){
Goods []arr1=new Goods[5];
arr1[0]=new Goods (23,"Lenovo");
arr1[1]=new Goods (53,"dell");
arr1[2]=new Goods (32,"HuaWei");
arr1[3]=new Goods (12,"LuoJi");
arr1[4]=new Goods (32,"Mi");
Arrays.sort (arr1, new Comparator<Goods> () {
@Override
public int compare(Goods o1, Goods o2) {
if(o1 instanceof Goods && o2 instanceof Goods){
Goods g1=(Goods)o1;
Goods g2=(Goods)o2;
if(g1.getName ().equals (g2.getName ())){
return -Double.compare (g1.getPrice (),g2.getPrice ());
}else
return g1.getName ().compareTo (g2.getName ());
}
throw new RuntimeException ("传入的数据类型有误");
}
});
System.out.println (Arrays.toString (arr1));
}
四、其他类
1.instant类的使用
@Test
public void instantTest(){
Instant instant = Instant.now ();
System.out.println (instant);
OffsetDateTime offsetDateTime = instant.atOffset (ZoneOffset.ofHours (8));
System.out.println (offsetDateTime);
long l = instant.toEpochMilli ();
System.out.println (l);
Instant instant1 = Instant.ofEpochMilli (1641393066234L);
System.out.println (instant1);
}
2.DateTimeFormatter
@Test
public void DateTimeFormatterTest(){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("yy-MM-dd hh:mm:ss");
String s = formatter.format (LocalDateTime.now ());
System.out.println (s);
TemporalAccessor parse = formatter.parse ("22-01-22 08:22:58");
System.out.println (parse);
}