javaSE-day04(常用API二)
1.StringBuilder类
StringBuilder代表可变字符串对象,相当于是一个容器,它里面的字符串是可以改变的,就是用来操作字符串的。
好处:StringBuilder比String更合适做字符串的修改操作,效率更高,代码也更加简洁。
方法演示
代码演示一下StringBuilder的用法
public class Test{
public static void main(String[] args){
StringBuilder sb = new StringBuilder("itehima");
//1.拼接内容
sb.append(12);
sb.append("字符串");
sb.append(true);
//2.append方法,支持临时编程
sb.append(666).append("字符串2").append(666);
System.out.println(sb); //打印:12字符串666字符串2666
//3.反转操作
sb.reverse();
System.out.println(sb); //打印:6662串符字666串符字21
//4.返回字符串的长度
System.out.println(sb.length());
//5.StringBuilder还可以转换为字符串
String s = sb.toString();
System.out.println(s); //打印:6662串符字666串符字21
}
}
应用案例
接下来,我们通过一个案例把StringBuilder运用下,案例需求如下图所示代码如下
public class Test{
public static void main(String[] args){
String str = getArrayData( new int[]{11,22,33});
System.out.println(str);
}
//方法作用:将int数组转换为指定格式的字符串
public static String getArrayData(int[] arr){
//1.判断数组是否为null
if(arr==null){
return null;
}
//2.如果数组不为null,再遍历,并拼接数组中的元素
StringBuilder sb = new StringBuilder("[");
for(int i=0; i<arr.length; i++){
if(i==arr.legnth-1){
sb.append(arr[i]).append("]");;
}else{
sb.append(arr[i]).append(",");
}
}
//3、把StirngBuilder转换为String,并返回。
return sb.toString();
}
}
2.StringJoiner类
前面使用StringBuilder拼接字符串的时,代码写起来还是有一点麻烦,而StringJoiner号称是拼接神器,不仅效率高,而且代码简洁。
下面演示一下StringJoiner的基本使用
public class Test{
public static void main(String[] args){
StringJoiner s = new StringJoiner(",");
s.add("java1");
s.add("java2");
s.add("java3");
System.out.println(s); //结果为: java1,java2,java3
//参数1:间隔符
//参数2:开头
//参数3:结尾
StringJoiner s1 = new StringJoiner(",","[","]");
s1.add("java1");
s1.add("java2");
s1.add("java3");
System.out.println(s1); //结果为: [java1,java2,java3]
}
}
使用StirngJoiner改写前面把数组转换为字符串的案例
public class Test{
public static void main(String[] args){
String str = getArrayData( new int[]{11,22,33});
System.out.println(str);
}
//方法作用:将int数组转换为指定格式的字符串
public static String getArrayData(int[] arr){
//1.判断数组是否为null
if(arr==null){
return null;
}
//2.如果数组不为null,再遍历,并拼接数组中的元素
StringJoiner s = new StringJoiner(", ","[","]");
for(int i=0; i<arr.length; i++){
//加""是因为add方法的参数要的是String类型
s.add(String.valueOf(arr[i]));
}
//3、把StringJoiner转换为String,并返回。
return s.toString();
}
}
3.Math类
Math是数学的意思,该类提供了很多个进行数学运算的方法,如求绝对值,求最大值,四舍五入等,话不多说,直接上代码。
public class MathTest {
public static void main(String[] args) {
// 目标:了解下Math类提供的常见方法。
// 1、public static int abs(int a):取绝对值(拿到的结果一定是正数)
// public static double abs(double a)
System.out.println(Math.abs(-12)); // 12
System.out.println(Math.abs(123)); // 123
System.out.println(Math.abs(-3.14)); // 3.14
// 2、public static double ceil(double a): 向上取整
System.out.println(Math.ceil(4.0000001)); // 5.0
System.out.println(Math.ceil(4.0)); // 4.0
// 3、public static double floor(double a): 向下取整
System.out.println(Math.floor(4.999999)); // 4.0
System.out.println(Math.floor(4.0)); // 4.0
// 4、public static long round(double a):四舍五入
System.out.println(Math.round(3.4999)); // 3
System.out.println(Math.round(3.50001)); // 4
// 5、public static int max(int a, int b):取较大值
// public static int min(int a, int b):取较小值
System.out.println(Math.max(10, 20)); // 20
System.out.println(Math.min(10, 20)); // 10
// 6、 public static double pow(double a, double b):取次方
System.out.println(Math.pow(2, 3)); // 2的3次方 8.0
System.out.println(Math.pow(3, 2)); // 3的2次方 9.0
// 7、public static double random(): 取随机数 [0.0 , 1.0) (包前不包后)
System.out.println(Math.random());
}
}
4.System类
系统类,提供了一些获取获取系统数据的方法。比如获取系统时间。
/**
* 目标:了解下System类的常见方法。
*/
public class SystemTest {
public static void main(String[] args) {
// 1、public static void exit(int status):
// 终止当前运行的Java虚拟机。
// 该参数用作状态代码; 按照惯例,非零状态代码表示异常终止。
System.exit(0); // 人为的终止虚拟机。(不要使用)
// 2、public static long currentTimeMillis():
// 获取当前系统的时间
// 返回的是long类型的时间毫秒值:指的是从1970-1-1 0:0:0开始走到此刻的总的毫秒值,1s = 1000ms
long time = System.currentTimeMillis();
System.out.println(time);
for (int i = 0; i < 1000000; i++) {
System.out.println("输出了:" + i);
}
long time2 = System.currentTimeMillis();
System.out.println((time2 - time) / 1000.0 + "s");
}
}
5.Runtime类
Java的运行时类,叫Runtime类。这个类可以用来获取JVM的一些信息,也可以用这个类去执行其他的程序。话不多少,上代码。
/**
* 目标:了解下Runtime的几个常见方法。
*/
public class RuntimeTest {
public static void main(String[] args) throws IOException, InterruptedException {
// 1、public static Runtime getRuntime() 返回与当前Java应用程序关联的运行时对象。
Runtime r = Runtime.getRuntime();
// 2、public void exit(int status) 终止当前运行的虚拟机,该参数用作状态代码; 按照惯例,非零状态代码表示异常终止。
// r.exit(0);
// 3、public int availableProcessors(): 获取虚拟机能够使用的处理器数。
System.out.println(r.availableProcessors());
// 4、public long totalMemory() 返回Java虚拟机中的内存总量。
System.out.println(r.totalMemory()/1024.0/1024.0 + "MB"); // 1024 = 1K 1024 * 1024 = 1M
// 5、public long freeMemory() 返回Java虚拟机中的可用内存量
System.out.println(r.freeMemory()/1024.0/1024.0 + "MB");
// 6、public Process exec(String command) 启动某个程序,并返回代表该程序的对象。
// r.exec("D:\\soft\\XMind\\XMind.exe");
Process p = r.exec("QQ");
Thread.sleep(5000); // 让程序在这里暂停5s后继续往下走!!
p.destroy(); // 销毁!关闭程序!
}
}
6.BigDecimal类
为了解决计算精度损失的问题,Java给我们提供了BigDecimal类,它提供了一些方法可以对数据进行四则运算,而且不丢失精度,同时还可以保留指定的小数位。下面看代码,演示一下
public class Test2 {
public static void main(String[] args) {
// 目标:掌握BigDecimal进行精确运算的方案。
double a = 0.1;
double b = 0.2;
// 1、把浮点型数据封装成BigDecimal对象,再来参与运算。
// a、public BigDecimal(double val) 得到的BigDecimal对象是无法精确计算浮点型数据的。 注意:不推荐使用这个,
// b、public BigDecimal(String val) 得到的BigDecimal对象是可以精确计算浮点型数据的。 可以使用。
// c、public static BigDecimal valueOf(double val): 通过这个静态方法得到的BigDecimal对象是可以精确运算的。是最好的方案。
BigDecimal a1 = BigDecimal.valueOf(a);
BigDecimal b1 = BigDecimal.valueOf(b);
// 2、public BigDecimal add(BigDecimal augend): 加法
BigDecimal c1 = a1.add(b1);
System.out.println(c1);
// 3、public BigDecimal subtract(BigDecimal augend): 减法
BigDecimal c2 = a1.subtract(b1);
System.out.println(c2);
// 4、public BigDecimal multiply(BigDecimal augend): 乘法
BigDecimal c3 = a1.multiply(b1);
System.out.println(c3);
// 5、public BigDecimal divide(BigDecimal b): 除法
BigDecimal c4 = a1.divide(b1);
System.out.println(c4);
// BigDecimal d1 = BigDecimal.valueOf(0.1);
// BigDecimal d2 = BigDecimal.valueOf(0.3);
// BigDecimal d3 = d1.divide(d2);
// System.out.println(d3);
// 6、public BigDecimal divide(另一个BigDecimal对象,精确几位,舍入模式) : 除法,可以设置精确几位。
BigDecimal d1 = BigDecimal.valueOf(0.1);
BigDecimal d2 = BigDecimal.valueOf(0.3);
BigDecimal d3 = d1.divide(d2, 2, RoundingMode.HALF_UP); // 0.33
System.out.println(d3);
// 7、public double doubleValue() : 把BigDecimal对象又转换成double类型的数据。
//print(d3);
//print(c1);
double db1 = d3.doubleValue();
double db2 = c1.doubleValue();
print(db1);
print(db2);
}
public static void print(double a){
System.out.println(a);
}
}
7.Date类
Date类,Java中是由这个类的对象用来表示日期或者时间。
Date对象记录的时间是用毫秒值来表示的。Java语言规定,1970年1月1日0时0分0秒认为是时间的起点,此时记作0,那么1000(1秒=1000毫秒)就表示1970年1月1日0时0分1秒,依次内推。
下面是Date类的构造方法,和常见的成员方法:
public class Test1Date {
public static void main(String[] args) {
// 目标:掌握Date日期类的使用。
// 1、创建一个Date的对象:代表系统当前时间信息的。
Date d = new Date();
System.out.println(d);
// 2、拿到时间毫秒值。
long time = d.getTime();
System.out.println(time);
// 3、把时间毫秒值转换成日期对象: 2s之后的时间是多少。
time += 2 * 1000;
Date d2 = new Date(time);
System.out.println(d2);
// 4、直接把日期对象的时间通过setTime方法进行修改
Date d3 = new Date();
d3.setTime(time);
System.out.println(d3);
}
}
8.SimpleDateFormat类
SimpleDateFormat类就可以转换Date对象表示日期时间的显示格式。
- 我们把Date对象转换为指定格式的日期字符串这个操作,叫做日期格式化,
- 反过来把指定格式的日期符串转换为Date对象的操作,叫做日期解析。
注意:创建SimpleDateFormat对象时,在构造方法的参数位置传递日期格式,而日期格式是由一些特定的字母拼接而来的。我们需要记住常用的几种日期/时间格式
字母 表示含义
yyyy 年
MM 月
dd 日
HH 时
mm 分
ss 秒
SSS 毫秒
"2022年12月12日" 的格式是 "yyyy年MM月dd日"
"2022-12-12 12:12:12" 的格式是 "yyyy-MM-dd HH:mm:ss"
按照上面的格式可以任意拼接,但是字母不能写错
最后,上代码演示一下
public class Test2SimpleDateFormat {
public static void main(String[] args) throws ParseException {
// 目标:掌握SimpleDateFormat的使用。
// 1、准备一些时间
Date d = new Date();
System.out.println(d);
long time = d.getTime();
System.out.println(time);
// 2、格式化日期对象,和时间 毫秒值。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
String rs = sdf.format(d);
String rs2 = sdf.format(time);
System.out.println(rs);
System.out.println(rs2);
System.out.println("----------------------------------------------");
// 目标:掌握SimpleDateFormat解析字符串时间 成为日期对象。
String dateStr = "2022-12-12 12:12:11";
// 1、创建简单日期格式化对象 , 指定的时间格式必须与被解析的时间格式一模一样,否则程序会出bug.
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d2 = sdf2.parse(dateStr);
System.out.println(d2);
}
}
9.Calendar类
Calendar类表示日历,它提供了一些比Date类更好用的方法。
常见方法:
因为Calendar类提供了方法可以直接对日历中的年、月、日、时、分、秒等进行运算。
public class Test4Calendar {
public static void main(String[] args) {
// 目标:掌握Calendar的使用和特点。
// 1、得到系统此刻时间对应的日历对象。
Calendar now = Calendar.getInstance();
System.out.println(now);
// 2、获取日历中的某个信息
int year = now.get(Calendar.YEAR);
System.out.println(year);
int days = now.get(Calendar.DAY_OF_YEAR);
System.out.println(days);
// 3、拿到日历中记录的日期对象。
Date d = now.getTime();
System.out.println(d);
// 4、拿到时间毫秒值
long time = now.getTimeInMillis();
System.out.println(time);
// 5、修改日历中的某个信息
now.set(Calendar.MONTH, 9); // 修改月份成为10月份。
now.set(Calendar.DAY_OF_YEAR, 125); // 修改成一年中的第125天。
System.out.println(now);
// 6、为某个信息增加或者减少多少
now.add(Calendar.DAY_OF_YEAR, 100);
now.add(Calendar.DAY_OF_YEAR, -10);
now.add(Calendar.DAY_OF_MONTH, 6);
now.add(Calendar.HOUR, 12);
now.set(2026, 11, 22);
System.out.println(now);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具