
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 ("**************");
//注意:replace换完后,必须有一个新的字符串去接收
String s7 = s5.replace ("fan", "YYY");
System.out.println (s7);
System.out.println (s6);
System.out.println ("**************");
}
}
2.string 的实例化
@Test
public void test2(){
//通过字面量定义的方式:此时的s1和s2的数据javaEE声明在方法区中的字符串常量池中。
String s1="fan";
String s2="fan";
//通过new +构造器的方式:此时的s3和s4保存的地址值,是数据在堆空间中开辟空间以后对
String s3=new String ("fan");
String s4=new String ("fan");
System.out.println (s1==s2);//true
System.out.println (s1==s3);//false
System.out.println (s3==s4);//false
System.out.println ("**********");
Person p1=new Person ("Tom",18);
Person p2=new Person ("Tom",20);
System.out.println (p1.name.equals (p2.name));//true
System.out.println (p1.name==p2.name);//true
}
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);//true
System.out.println (s3==s5);//false
System.out.println (s3==s7);//false
System.out.println (s5==s6);//false
System.out.println (s5==s7);//false
String s8 = s5.intern ();///返回值得到的s8使用的常量值中已经存在的"Tomcat"
System.out.println (s8==s3);//true
}
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);//best
System.out.println (stringTest2.str);//good
}
}
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));//从特定位置i到特定位置j
}
}
常用方法二:
@Test
public void test2(){
String s1="HelloWord";
System.out.println (s1.endsWith ("ord"));//后缀是否是以str结束
System.out.println (s1.startsWith ("He"));//是否是以str开头
System.out.println (s1.startsWith ("lo",3));//指定位置i是否是以str开头
System.out.println (s1.contains ("ord"));//字符串中是否包含str
System.out.println (s1.indexOf ("lo"));//返回str在字符串中第一次出现的位置
System.out.println (s1.indexOf ("lo",2));//从i位置开始寻找str第一次出现的位置
System.out.println ("*************");
System.out.println (s1.lastIndexOf ("or"));//从末尾反向搜索
System.out.println (s1.lastIndexOf ("or",7));//从i位置反向搜索
// 注意:indexOf和lastIndexOf方法如果未找到都是返回-1
}
常用方法三:
@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类与包装类的转换
//string 与基本数据类型、包装类之间的转换。|
//String -->基本数据类型、包装类:调用包装类的静态方法: parseXxx( str)
// 基本数据类型、包装类-->string:调用string重载的vaLue0f(xxx)
public class StringTest4 {
@Test
public void test(){
String s1="123";
//String -->基本数据类型、包装类:调用包装类的静态方法: parseXxx( str)
int i = Integer.parseInt (s1);
System.out.println (i);
System.out.println ("*************");
// 基本数据类型、包装类-->string:调用string重载的vaLue0f(xxx)
String s = String.valueOf (i);//写法一
System.out.println (s);
String s2=""+i;//写法二
System.out.println (s2);
}
}
7.string类与char[]的转换
//string类与char[]的转换
//string --> char[]:调用string的toCharArray()
// char[] --> string:调用string的构造器
@Test
public void test2(){
String s1="fan1116";
char[] chars = s1.toCharArray ();
for(int i=0;i
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.三者效率比较
对比string,StringBuffer.StringBuilder三者的效率
–>从高到低排列: StringBuilder > stringBuffer > string
13.System类中获取时间的方法
//System类中的currentTimeMillis()
package Test03;
import org.junit.Test;
import java.util.Date;
public class TimeTest {
@Test
public void test2(){
//构造器一: Date():创建一个对应当前时间的Date对象,>toString():显示当前的年、月、日、时、分、秒
Date date=new Date();
System.out.println (date.toString ());//Fri Dec 03 16:25:20 CST 2021
System.out.println (date.getTime ());//1638519995369
//构造器二:创建指定毫秒数的Date对象
Date date1=new Date (1638519995369L);
System.out.println (date1.toString ());//Fri Dec 03 16:25:20 CST 2021
System.out.println (date1.getTime ());//1638519995369
}
//1.System类中的currentTimeMillis()
@Test
public void test1(){
//返回当前时间与1970年1月1日0时e分日秒之间以毫秒为单位的时间差。
long time = System.currentTimeMillis ();
System.out.println (time );
}
}
二、时间类
1. System类中currentTimeMillis()
//1.System类中的currentTimeMillis()
@Test
public void test1(){
//返回当前时间与1970年1月1日0时e分日秒之间以毫秒为单位的时间差。
long time = System.currentTimeMillis ();
System.out.println (time );
}
2.Date类
@Test
public void test2(){
//构造器一: Date():创建一个对应当前时间的Date对象,>toString():显示当前的年、月、日、时、分、秒
Date date=new Date();
System.out.println (date.toString ());//Fri Dec 03 16:25:20 CST 2021
System.out.println (date.getTime ());//1638519995369
//构造器二:创建指定毫秒数的Date对象
Date date1=new Date (1638519995369L);
System.out.println (date1.toString ());//Fri Dec 03 16:25:20 CST 2021
System.out.println (date1.getTime ());//1638519995369
}
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 {
//格式化format:
//按照指定的方式进行格式化和解析:调用带参构造器
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-DD hh:mm:ss");
Date date=new Date ();
String s = sdf.format (date );
System.out.println (s);
//解析parse:
// 格式化的逆过程: 字符串-->日期
Date date2 = sdf.parse (s);
System.out.println (date2);
}
//练习:字符串"2028-09-08"转换为java.sqL.Date
@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);
}
//练习二:
// "三天打渔两天晒网" 1990-01-01 开始打渔,问在XXXX-XX-XX时是打鱼还是晒网?
//举例:2019-11-16 ? 是打鱼还是晒网
//总天数% 5 == 1,2,3 ∶打渔
// 总天数% 5 == 4,0 :晒网
// 总天数的计算? -->计算出来的是总天数
//方式: ( date2.getTime() - date1.getTime())/(1000 * 60* 60* 24) +1
@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;
// System.out.println (l);
if(l==1||l==2||l==3){
System.out.println (end+"在打渔");
}else if(l==0||l==4){
System.out.println ("end+在晒网");
}
}
}
4.Calendar(日历)类
//Calendar (抽象的)日历类---->jdk8.0以前
@Test
public void CalendarTest(){
//1.实例化 -->常用方法二
///方式一:创建其子类(GregorianCalendar)的对象
// 方式二:调用其静态方法getInstance()
Calendar calendar = Calendar.getInstance ();
//2.常用方法
// get()
int day = calendar.get (Calendar.DAY_OF_MONTH);
System.out.println (day);
// set()
calendar.set (DAY_OF_MONTH,22);
day = calendar.get (DAY_OF_MONTH);
System.out.println (day);
// add()
calendar.set (DAY_OF_MONTH,3);
day = calendar.get (DAY_OF_MONTH);
System.out.println (day);
// getTime() 日历类-->Date
Date date = calendar.getTime ();
System.out.println (date);
// setTime() Date -->日历类
Date date1 = new Date ();
calendar.setTime (date1);
day= calendar.get (Calendar.DAY_OF_MONTH);
System.out.println (day);
}
5.LocalDateTime 的使用
//LocalDate. LocalTime、LocalDateTime 的使用
//LocalDate. LocalTime使用同LocalDateTime一样
@Test
public void LocalDateTime(){
//now() 获取现在是 日期+地点
LocalDateTime localDateTime = LocalDateTime.now ();
System.out.println (localDateTime);
//of() 设置 日期+时间
LocalDateTime localDateTime1 = LocalDateTime.of (2019, 11, 16, 11, 28);
System.out.println (localDateTime);
System.out.println (localDateTime1);
System.out.println ("************************");
//getXxx 获得..... 没有偏移量
System.out.println (localDateTime.getMonthValue ());
System.out.println (localDateTime.getDayOfMonth ());
System.out.println ("************************");
//withXxx 修改日期、时间------>不可变性
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 ("************************");
//plusXxx 给xx增加多少------>不可变性
LocalDateTime localDateTime4 = localDateTime.plusDays (5);
System.out.println (localDateTime);
System.out.println (localDateTime4);
//minusXxx 给Xxx减少多少 ------>不可变性
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);
// //方式二:
// return Double.compare (this.price,goods.price);
}
//如果不是个商品,则抛出异常
throw new RuntimeException ("传入的数据类型不一致!");
}
}
2.Comparator
@Test
public void ComparatorTest(){
String []arr={"DD","AA","FF","CC","BB","EE"};
Arrays.sort (arr, new Comparator () {
@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 () {
@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(){
//now 输出本初子午线的时间
Instant instant = Instant.now ();
System.out.println (instant);
//atOffset设置偏移量
OffsetDateTime offsetDateTime = instant.atOffset (ZoneOffset.ofHours (8));
System.out.println (offsetDateTime);
//toEpochMilli 获取自1970年1月1日0时0分日秒(UTC)开始的毫秒数
long l = instant.toEpochMilli ();
System.out.println (l);
//通过给定的毫秒数,获取Instant实例 -->Date(Long millis)
Instant instant1 = Instant.ofEpochMilli (1641393066234L);
System.out.println (instant1);
}
2.DateTimeFormatter
//类似于SimpleDateFormat类
@Test
public void DateTimeFormatterTest(){
//自定义的格式 ofPattern("yy-MM-dd hh:mm:ss")
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("yy-MM-dd hh:mm:ss");
//格式化:
String s = formatter.format (LocalDateTime.now ());
System.out.println (s);//22-01-22 08:22:58
//解析:
TemporalAccessor parse = formatter.parse ("22-01-22 08:22:58");
System.out.println (parse);//{MinuteOfHour=22, SecondOfMinute=58, MicroOfSecond=0, NanoOfSecond=0, MilliOfSecond=0, HourOfAmPm=8},ISO resolved to 2022-01-22
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)