JAVA学习笔记07——常用类

这篇博客介绍了JAVA编程中关于包装类的基本概念,包括自动装箱和拆箱,以及-128到127之间的缓存处理。详细讲解了String类的方法,如concat()、substring()等,并提到了可变字符序列StringBuilder和StringBuffer,建议在非多线程环境下使用StringBuilder以提高效率。此外,还阐述了Date、DateFormat和Calendar类在时间处理上的应用,包括日期的获取、设置、比较和转换。最后简单提及了File类在文件操作中的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.包装类

1.JAVA8个基本数据类型都有其对应的包装类,包装类的用途有两个:1.方便涉及对象的存储2.包含了基本数据类型的各种功能(如字符串,基本数据类型,包装类的相互转化)。

// 基本类型转化成Integer对象

Integer int1 = Integer.valueOf(20);

// Integer对象转化成int

int a = int1.intValue();

// 字符串转化成Integer对象

Integer int3 = Integer.parseInt("334");

// Integer对象转化成字符串

String str1 = int3.toString();

2.自动装箱与自动拆箱:包装类和基本数据类型之间的相互转化。

3.包装类在自动装箱时为了提高效率,对于-128~127之间的值会进行缓存处理。超过范围后,对象之间不能再使用==进行数值的比较,而是使用equals方法。

2.String类

1. String类的下述方法能创建并返回一个新的String对象: concat()、 replace()、substring()、 toLowerCase()、 toUpperCase()、trim()。

2. 提供查找功能的有关方法: endsWith()、 startsWith()、 indexOf()、lastIndexOf()。

3. 提供比较功能的方法: equals()、equalsIgnoreCase()、compareTo()。

3.可变字符序列StringBuffer和StringBuilder类

1. StringBuffer,线程安全,做线程同步检查, 效率较低。

2. StringBuilder,线程不安全,不做线程同步检查,因此效率较高。 建议采用该类。

常用方法:

StringBuilder append()

StringBuilder delete(int start,int end)

StringBuilder deleteCharAt(int index)

StringBuilder insert(…):为该StringBuilder 对象在指定位置插入字符序列,仍然返回自身对象。

StringBuilder reverse()

String toString() 

4.时间处理相关类

1. Date时间类

Date date1 = new Date(); 给date1指定当前时间(Tue May 03 19:30:02 GMT+08:00 2022)

data1.getTime:返回从1970 年 1 月 1 日 00:00:00到现在的秒数。

after(Date when) 测试此日期是否在指定日期之后。

before(Date when) 测试此日期是否在指定日期之前。

equals(Object obj) 比较两个日期的相等性。

2.DateFormat类和SimpleDateFormat类

DateFormat把时间对象转化成指定格式的字符串。反之,把指定格式的字符串转化成时间对象。DateFormat是一个抽象类,一般使用它的的子类SimpleDateFormat类来实现。

public class TestDataFormat {
    public static void main(String[] args) throws ParseException {
        // new出SimpleDateFormat对象
        SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
        // 将时间对象转换成字符串
        System.out.println(s1.format(new Date()));
        System.out.println(new SimpleDateFormat("hh:mm:ss").format(new Date()));
        // 将符合指定格式的字符串转成成时间对象.字符串格式需要和指定格式一致。
        String time = "2022-05-02";
        Date date = s1.parse(time);
        System.out.println("date1: " + date);
    }
}

DateFormat指定了一个标准格式,具体内容需要Date类来赋值

.format : 将时间对象转换为字符串

str = testDataFormat.format(new Date())

.parse :将字符串转换为时间对象Date

testDate =  = testDataFormat.parse(str) (格式和DateFormat的格式一样)

3.Calendar日历类

 Calendar 类是一个抽象类,为我们提供了关于日期计算的相关功能,比如:年、月、日、时、分、秒的展示和计算。GregorianCalendar 是 Calendar 的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。

import java.util.*;
public class TestCalendar {
    public static void main(String[] args) {
        // 得到相关日期元素
        GregorianCalendar calendar = new GregorianCalendar(2021, 10, 9, 22, 10, 50);
        int year = calendar.get(Calendar.YEAR); // 打印:2021
        System.out.println(year);
        // 设置日期
        GregorianCalendar calendar2 = new GregorianCalendar();
        calendar2.set(Calendar.YEAR, 2122);
        System.out.print("calendar2:"+'\t');
        printCalendar(calendar2);
        // 日期计算
        GregorianCalendar calendar3 = new GregorianCalendar(2023, 10, 9, 22, 10, 50);
        calendar3.add(Calendar.MONTH, -7); // 月份减7
        calendar3.add(Calendar.DATE, 7); // 增加7天
        System.out.print("calendar3:"+'\t');
        printCalendar(calendar3);
        // 日历对象和时间对象转化
        Date date = calendar3.getTime();
        System.out.println("date:"+'\t'+date);
        GregorianCalendar calendar4 = new GregorianCalendar();
        calendar4.setTime(new Date());
        System.out.print("calendar4:"+'\t');
        printCalendar(calendar4);
    }
    static void printCalendar(Calendar calendar) {
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int date = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 星期几
        String week = "" + ((date == 0) ? "日" : date);
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        System.out.printf("%d年%d月%d日,星期%s %d:%d:%d\n", year, month, day,
                week, hour, minute, second);
    }
}

 

.get 和  Calendar.YEAR 等配饰使用,得到日期等参数。

.set 和  Calendar.YEAR 等配合使用,设置日期等参数。

Date和Calendar相互转化:

Calendar --> Date : Date testDate = testCalendar.getTime();

Date --> Calendar:testCalendar.setTime(new Date());

5.File类的使用

File file = new File("d:/jywang/master.txt");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值