String类
- String类:代表字符串
- String类是一个final类,不能被继承,不可变字符序列
- 当对字符串重新赋值时,需要重写指定内容区域赋值,不能使用原有value赋值
- 当现有字符串进行连接操作时,也需要重新指定内存区域赋值,不能使用原有value赋值
- 当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能使用原有value赋值
- 字符串是个常量,用双引号引起来表示,值在创建后不能更改
- String对象的字符内容是存储在一个字符数组value中
String类常用方法
方法 |
含义 |
int length() |
返回字符串长度 |
char charAt(int index) |
返回某处索引的字符 |
boolean isEmpty |
判断是否是空字符串 |
String toLowerCase() |
将字符转换为小写 |
String toUpperCase() |
将字符转换为大写 |
String trim() |
返回字符串副本,忽略前导空白和尾部空白 |
boolean equals(Object Obj) |
比较字符串内容是否相同 |
boolean equalslgnoreCase(String anotherString) |
与equals类似,但忽略大小写 |
String concat(String str) |
将指定字符串连接到次字符串结尾 |
int compareTo(String anotherString) |
比较两个字符串大小 |
String subString(int beginIndex) |
返回一个新的字符串,它是从beginIndex开始截取到最后一个字符串 |
String subString(int beginIndex,int endIndex) |
返回一个新的字符串,它是从beginIndex开始到endIndex(不包含)结束的字符串 |
boolean startsWith(String suffix) |
测试字符串是否以指定字符串开始 |
boolean startsWith(String prefix,int toffset) |
测试字符串指定位置字符串是否以指定前缀开始 |
boolean endsWith(String prefix) |
测试字符串是否以指定字符串结束 |
boolean contains(CharSequence s) |
当且仅当此字符串包含指定的char值序列时,返回true |
int IndexOf(String str) |
返回此字符第一次出现时的索引 |
int IndexOf(String str,int fromIndex) |
返回此字符第一次出现时的索引,从指定索引开始 |
int lastIndexOf(String str) |
返回此字符最右边出现时的索引 |
int lastIndexOf(String str,int fromIndex) |
此字符最后一次出现的索引,从指定索引开始反向搜索 |
String replace(char oldChar,char newChar) |
返回一个新字符串,他是newChar替换所有的oldChar后得到的 |
String replace(CharSequence target,CharSequence replacement) |
使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串 |
String replaceAll(String regex,String replacement) |
使用给定的replacement替换所有匹配的正则表达式的子字符串 |
String replaceFirst(String regex,String replacement) |
使用给定的replacement替换所有匹配的正则表达式的第一个子字符串 |
boolean matches(String regex) |
告知此字符串是否匹配给定的正则表达式 |
String[] split(String regex) |
根据给定的正则表达式拆分字符串 |
String[] split(String regex,int limit) |
根据给定的正则表达式拆分字符串,但不能超过limit个,多的都放在最后一个里 |
String类与基本数据转换
字符串=>基本数据类型,包装类
基本数据类型=>包装类
- 调用String重载的valueOf(XXX)
- 用连接符“+”
String与字符数组之间转换
- String=>char[]
str.toCharArray()
- char[]=>String
调用String构造器
String与byte[]字节之间转换
- String=>byte[]之间的转换
调用String的getBytes()
- byte[]=>String之间的转换
调用String的构造器
StringBuffer类
方法 |
含义 |
append(XXX) |
添加元素 |
delete(int start,int end) |
删除元素 |
replace(int start,int end,String str) |
替换元素 |
insert(int offset,XXX) |
在指定位置插入 |
reverse() |
当前字符序列反转 |
subString(int start,int end) |
返回一个从start开始到end结束(不包括)的一个子字符串 |
length() |
返回数组长度 |
charAt(int n) |
返回n的字符 |
setCharAt(int n,char ch) |
修改一个字符 |
StringBuilder类
- 可变的字符序列
- 线程不安全,效率高
- 方法和StringBuffer类一致
日期时间API
java.lang.System类
- System.currentTimeMillis()
- 用来返回当前日期距离1970年1月1日0时0分0秒以毫秒为单位的时间差
java.util.Date类
- Date():无参构造器可以获取本地时间
- Date(long date)
- getTime():返回自1970年1月1日00:00:00GMT以此Date对象的毫秒数
- toString():把Date转换为以下形式的String:dow mon dd hh:mm:ss zzz yyy
java.sql.Date类
@Test
public void test8(){
java.sql.Date date2= new java.sql.Date(1665110725360L);
System.out.println(date2);
}
java.util.Date类转换为java.sql.Date类
java.util.Date date3=new java.util.Date();
java.sql.Date date4=new java.sql.Date(date3.getTime());
System.out.println(date4);
SimpleDateFormat类
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
@Test
public void testSimpleDateFormat() throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat();
Date date=new Date();
System.out.println(date);
String format=sdf.format(date);
System.out.println(format);
String str="2022/11/8 下午9:14";
Date date2=sdf.parse(str);
System.out.println(date2);
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format1=sdf1.format(date);
System.out.println(format1);
String str1="2000-11-11 11:11:11";
Date date3=sdf1.parse(str1);
System.out.println(date3);
}
}
Calendar类
实例化对象
Calendar calendar=Calendar.getInstance();
常用方法
Calendar calendar=Calendar.getInstance();
int day1=calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day1);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
calendar.set(Calendar.DAY_OF_MONTH,10);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.DAY_OF_MONTH,10);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
Date date1=calendar.getTime();
System.out.println(date1);
Date date2=new Date();
calendar.setTime(date2);
int day3=calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day3);
java8新的API
LocalDate,LocalTime,LocalDateTime
import org.junit.Test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class java8DateTime {
@Test
public void test(){
LocalDate localDate= LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
LocalDateTime localDateTime1 = LocalDateTime.of(2022, 11, 9, 21, 39, 29);
System.out.println(localDateTime1);
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getMonth());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getMinute());
LocalDateTime localDateTime2 = localDateTime.withHour(2);
System.out.println(localDateTime);
System.out.println(localDateTime2);
LocalDateTime localDateTime3 = localDateTime.plusDays(3);
System.out.println(localDateTime3);
LocalDateTime localDateTime4 = localDateTime.minusHours(2);
System.out.println(localDateTime4);
}
}
instant类
import org.junit.Test;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class InstantTest {
@Test
public void instantTest(){
Instant instant=Instant.now();
System.out.println(instant);
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
long toEpochMilli = instant.toEpochMilli();
System.out.println(toEpochMilli);
Instant instant1 = instant.ofEpochMilli(1668433839379L);
System.out.println(instant1);
}
}
DateTimeFormatter
预定义格式
DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime now = LocalDateTime.now();
String str=isoLocalDateTime.format(now);
System.out.println(now);
System.out.println(str);
TemporalAccessor parse = isoLocalDateTime.parse("2022-11-14T22:02:23.117");
System.out.println(parse);
本地相关格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
String format = dateTimeFormatter.format(now);
System.out.println(format);
自定义格式
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String format1 = dateTimeFormatter1.format(LocalDateTime.now());
System.out.println(format1);
TemporalAccessor parse1 = dateTimeFormatter1.parse("2022-11-14 10:21:14");
System.out.println(parse1)
Java比较器
- Java中的对象,正常情况下我们只能进行==和!=
- 比较大小需要实现compareTo(obj)
compareTo接口
- 像Sting,包装类等实现了comparable接口,重写了compareTo接口
重写compareTo接口规则
- 如果当前对象this大于形参对象obj,返回正整数
- 如果当前对象this小于形参对象obj,返回负整数
- 如果当前对象this等于形参对象obj,返回0
import org.junit.Test;
import java.util.Arrays;
public class CompareTest {
@Test
public void test1(){
Goods[] arr=new Goods[4];
arr[0]=new Goods("mouse1",34);
arr[1]=new Goods("mouse1",24);
arr[2]=new Goods("mouse1",44);
arr[3]=new Goods("mouse1",14);
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
class Goods implements Comparable{
String name;
double price;
public Goods(String name,double price){
this.name=name;
this.price=price;
}
public String getName(){
return this.name;
}
@Override
public String toString(){
return "name:"+this.name+"price:"+this.price;
}
@Override
public int compareTo(Object o) {
if(o instanceof Goods){
Goods goods=(Goods) o;
if(this.price> goods.price){
return 1;
} else if (this.price< goods.price) {
return -1;
}else {
return 0;
}
}
throw new RuntimeException("输入有误");
}
}
comparator接口
- 当元素没有实现java.lang.Comparable接口,而又不方便更改代码时
- 当元素实现java.lang.Comparable接口,但排序规则不适合当前操作
重写compare(Object o1,Object o2)
- 如果方法返回正整数,则o1大于o2
- 如果返回零,则o1等于o2
- 如果方法返回负整数,则o1小于o2
public void test2(){
String[] arr=new String[]{
"AA","BB","SS","CC"};
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));
}
System类
- 代表系统,系统中很多属性和方法都放置在该类的内部
- 该类是私有的,无法创造该类的对象,无法实例化该类
- 内部成员方法都是static的
属性名 |
属性说明 |
java.version |
java运行时环境版本 |
java.home |
java安装目录 |
os.name |
操作系统名称 |
os.version |
操作系统版本 |
user.name |
用户账户名称 |
user.home |
用户的主目录 |
user.dir |
用户当前工作目录 |
public void test1(){
String javaVersion=System.getProperty("java.version");
System.out.println(javaVersion);
String javaHome=System.getProperty("java.home");
System.out.println(javaHome);
String osName=System.getProperty("os.name");
System.out.println(osName);
String osVersion=System.getProperty("os.version");
System.out.println(osVersion);
String userName=System.getProperty("user.name");
System.out.println(userName);
String userHome=System.getProperty("user.home");
System.out.println(userHome);
String userDir=System.getProperty("user.dir");
System.out.println(userDir);
}
Math类
- Math类提供了一系列静态方法用于科学计算
- 其返回值一般为double类型
属性名 |
属性说明 |
abs |
绝对值 |
acos,asin,atan,sin,cos,tan |
三角函数 |
sqrt |
平方根 |
pow(double a,doble b) |
a的b次幂 |
log |
自然对数 |
exp |
e为底的指数 |
max(double a,double,b) |
最大值 |
min(double a,double b) |
最小值 |
random() |
返回0到1的随机数 |
long round(double a) |
double 数据类型转换为lang(四舍五入) |
toDegrees(double angrad) |
弧度转角度 |
toRadians(double angdeg) |
角度转弧度 |