可变字符串类和日期相关类

2021-01-04  本文已影响0人  阿木木笔记

来自拉钩教育-JAVA就业集训营

可变字符串类和日期相关类

可变字符串类(重点)

基本概念

StringBuilder类常用的构造方法

方法声明 功能介绍
StringBuilder() 使用无参方式构造对象,容量为16
StringBuilder(int capacity) 根据参数指定的容量来构造对象,容量为参数指定大小
StringBuilder(String str) 根据参数指定的字符串来构造对象,容量为:16+字符串长度
package com.lagou.task13;

public class StringBuilderTest {
    public static void main(String[] args) {
        // 1.使用无参方式构造StringBuilder类型的对象并打印容量和长度
        StringBuilder sb1 = new StringBuilder();
        System.out.println("sb1 = " + sb1); // 自动调用toString方法 啥也没有
        System.out.println("容量是:" + sb1.capacity()); // 16
        System.out.println("长度是:" + sb1.length()); // 0

        System.out.println("-----------------------------------------------");
        // 2.使用参数指定的容量来构造对象并打印容量和长度
        StringBuilder sb2 = new StringBuilder(20);
        System.out.println("sb2 = " + sb2); // 自动调用toString方法 啥也没有
        System.out.println("容量是:" + sb2.capacity()); // 20
        System.out.println("长度是:" + sb2.length()); // 0

        System.out.println("-----------------------------------------------");
        // 2.根据参数指定的字符串内容来构造对象并打印容量和长度
        StringBuilder sb3 = new StringBuilder("hello");
        System.out.println("sb3 = " + sb3); // 自动调用toString方法 hello
        System.out.println("容量是:" + sb3.capacity()); // 16 + 5 = 21
        System.out.println("长度是:" + sb3.length()); // 5
    }
}

StringBuilder类常用的成员方法

方法声明 功能介绍
int capacity() 用于返回调用对象的容量
int length() 用于返回字符串的长度,也就是字符的个数
StringBuilder insert(int offset, String str) 插入字符串并返回调用对象的引用,就是自己。
StringBuilder append(String str) 追加字符串
StringBuilder deleteCharAt(int index) 将当前字符串中下标为index位置的单个字符删除
StringBuilder delete(int start,int end) 删除字符串
StringBuilder replace(int start,int end,String str) 替换字符串
StringBuilder reverse() 字符串反转
package com.lagou.task13;

public class StringBuilderTest {
    public static void main(String[] args) {
        // 1.使用无参方式构造StringBuilder类型的对象并打印容量和长度
        StringBuilder sb1 = new StringBuilder();
        System.out.println("sb1 = " + sb1); // 自动调用toString方法 啥也没有
        System.out.println("容量是:" + sb1.capacity()); // 16
        System.out.println("长度是:" + sb1.length()); // 0

        System.out.println("-----------------------------------------------");
        // 2.使用参数指定的容量来构造对象并打印容量和长度
        StringBuilder sb2 = new StringBuilder(20);
        System.out.println("sb2 = " + sb2); // 自动调用toString方法 啥也没有
        System.out.println("容量是:" + sb2.capacity()); // 20
        System.out.println("长度是:" + sb2.length()); // 0

        System.out.println("-----------------------------------------------");
        // 3.根据参数指定的字符串内容来构造对象并打印容量和长度
        StringBuilder sb3 = new StringBuilder("hello");
        System.out.println("sb3 = " + sb3); // 自动调用toString方法 hello
        System.out.println("容量是:" + sb3.capacity()); // 16 + 5 = 21
        System.out.println("长度是:" + sb3.length()); // 5

        System.out.println("-----------------------------------------------");
        // 4.实现向字符串中插入和追加字符串内容
        // 向下标为0的位置插入字符串内容"abcd",也就是向开头位置插入字符串内容
        StringBuilder sb4 = sb3.insert(0, "abcd");
        System.out.println("sb4 = " + sb4); // abcdhello 返回调用对象自己的引用,也就是返回值和调用对象时同一个字符串
        System.out.println("sb3 = " + sb3); // abcdhello
        // 向中间位置插入字符串"1234"
        sb3.insert(4, "1234");
        System.out.println("sb3 = " + sb3); // abcd1234hello
        // 向末尾位置插入字符串"ABCD"
        sb3.insert(sb3.length(), "ABCD");
        System.out.println("sb3 = " + sb3); // abcd1234helloABCD
        // 向末尾位置最佳字符串内容
        sb3.append("world");
        System.out.println("sb3 = " + sb3); // abcd1234helloABCDworld
        // 当字符串的长度超过了字符串对象的初始容量时,该字符串对象会在佛那个扩容,默认扩容算法是:
        // 原始容量 * 2 + 2 => 21 * 2 + 2 => 44
        // 底层采用byte数组来存放所有的字符内容
        System.out.println("容量是:" + sb3.capacity()); // 44
        System.out.println("长度是:" + sb3.length()); // 22

        System.out.println("-----------------------------------------------");
        // 5.实现字符串中字符和字符串的删除
        // 表示删除下标为8的单个字符
        sb3.deleteCharAt(8);
        System.out.println("sb3 = " + sb3); // abcd1234elloABCDworld
        // 使用for循环删除多个字符
        for (int i = 8; i < 12; i++) {
            // 有结果可知:删除一个字符后就跳过一个字符继续删除,因为每当删除一个字符后后面的字符会向前补位,因为下标会发生变化
//            sb3.deleteCharAt(i);
            // 始终删除下标为8的字符
            sb3.deleteCharAt(8);
        }
        System.out.println("删除后的字符串是:" + sb3); // abcd1234ABCDworld

        System.out.println("-----------------------------------------------");
        // 删除起始字符串abcd,包含0但不包含4
        sb3.delete(0, 4);
        System.out.println("sb3 = " + sb3); // 1234ABCDworld
        // 删除中间字符串
        sb3.delete(4, 8);
        System.out.println("sb3 = " + sb3); // 1234world
        // 删除末尾字符串
        sb3.delete(4, sb3.length());
        System.out.println("sb3 = " + sb3); // 1234

        System.out.println("-----------------------------------------------");
        // 6.实现字符串内容的修改、查找以及反转操作
        // 表示将下标为0这个位置的字符修改为'a'
        sb3.setCharAt(0, 'a');
        System.out.println("修改单个字符后的内容是:" + sb3); // a234
        // 修改 "234" 为 "bcd"
        sb3.replace(1, 4, "bcd");
        System.out.println("修改字符串后的结果是:" + sb3); // abcd
        // 实现查找功能
        int pos = sb3.indexOf("b");
        System.out.println("从前向后查找的结果是:" + pos); // 1
        pos = sb3.lastIndexOf("b");
        System.out.println("从后向前查找的结果是:" + pos); // 1
        // 实现字符串的反转功能
        sb3.reverse();
        System.out.println("反转后的结果是:" + sb3); // dcba
    }
}
package com.lagou.task13;

public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb1 = new StringBuilder("1");
        StringBuilder sb2 = new StringBuilder("2");
        show(sb1, sb2);
        System.out.println(sb1 + "," + sb2); // 12,2
    }

    static void show(StringBuilder x, StringBuilder y) {
        x.append(y);
        y = x;
    }
}

返回值的设计

Java8之前的日期相关类(熟悉)

System类的概述

基本概念

常用的方法

方法声明 功能介绍
static longcurrentTimeMillis() 返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差
package com.lagou.task13;

public class SystemTest {
    public static void main(String[] args) {
        // 1.获取当前系统时间距离1970年1月1日0时0分0秒的毫秒数
        long msec = System.currentTimeMillis();
        // 当前系统时间距离1970年1月1日0时0分0秒已经过去了1609667101573毫秒了!
        System.out.println("当前系统时间距离1970年1月1日0时0分0秒已经过去了" + msec + "毫秒了!");
        // 通常用于测试某一段代码的执行效率
    }
}

Date类的概述

基本概念

常用的方法

方法声明 功能介绍
Date() 使用无参的方式构造对象,也就是当前系统时间
Date(long date) 根据参数指定毫秒数构造对象, 参数为距离1970年1月1日0时0分0秒的毫秒数
long getTime() 获取调用对象距离1970年1月1日0时0分0秒的毫秒数
void setTime(longtime) 设置调用对象为距离基准时间time毫秒的时间点
package com.lagou.task13;

import java.util.Date;

public class DateTest {
    public static void main(String[] args) {
        // 1.使用无参方式构造Date对象并打印
        Date d1 = new Date();
        System.out.println("d1 = " + d1);// 获取当前系统时间 Sun Jan 03 17:50:25 CST 2021

        System.out.println("-------------------------------------------");
        // 2.使用参数指定的毫秒数来构造Date对象并打印 1秒 = 1000毫秒  东八区
        Date d2 = new Date(1000);
        System.out.println("d2 = " + d2); // Thu Jan 01 08:00:01 CST 1970

        System.out.println("-------------------------------------------");
        // 3.获取调用对象距离1970年1月1日0时0分0秒的毫秒数
        long msec = d2.getTime();
        System.out.println("获取到的毫秒数是:" + msec); // 1000

        // 4.设置调用对象所表示的时间点为参数指定的毫秒数
        d2.setTime(2000);
        System.out.println("修改后的时间是:" + d2); // Thu Jan 01 08:00:02 CST 1970
    }
}

SimpleDateFormat类的概述

基本概念

常用的方法

方法声明 功能介绍
SimpleDateFormat() 使用无参方式构造对象
SimpleDateFormat(Stringpattern) 根据参数指定的模式来构造对象,模式主要有: y-年 M-月 d-日H-时 m-分 s-秒
final String format(Datedate) 用于将日期类型转换为文本类型
Date parse(String source) 用于将文本类型转换为日期类型
package com.lagou.task13;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatTest {
    public static void main(String[] args) throws Exception{
        // 1.获取当前系统时间并打印
        Date d1 = new Date();
        System.out.println("d1 = " + d1); // Sun Jan 03 18:02:58 CST 2021
        // 2.构造SimpleDateFormat类型的对象并指定格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 3.实现日期类型向文本类型的转换并打印
        String format = sdf.format(d1);
        System.out.println("转换后的日期为:" + format); // 2021-01-03 18:02:58
        // 4.实现文本类型到日期类型的转换并打印
        Date parse = sdf.parse(format);
        System.out.println("转回日期格式的结果为:" + parse); // Sun Jan 03 18:02:58 CST 2021
    }
}

Calendar类的概述

基本概念

常用的方法

方法声明 功能介绍
static Calendar getInstance() 用于获取Calendar类型的引用
void set(int year, int month, int date, int hourOfDay, intminute, int second) 用于设置年月日时分秒信息
Date getTime() 用于将Calendar类型转换为 Date类型
void set(int field, int value) 设置指定字段的数值
void add(int field, int amount) 向指定字段增加数值
package com.lagou.task13;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarTest {
    public static void main(String[] args) {
        // 1.使用过时的方法按照指定的年月日时分秒来构造对象
//        Date d1 = new Date(2008, 8, 8, 20, 8, 8);
        Date d1 = new Date(2008 - 1900, 8 - 1, 8, 20, 8, 8);
        // 2.设置日期对象的格式并打印
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        String format = sdf.format(d1);
//        System.out.println("获取到的时间是:" + format); // 3908-09-08 20:08:08
        System.out.println("获取到的时间是:" + format); // 2008-08-08 20:08:08

        System.out.println("----------------------------------------------------------");
        // 2.使用取代的方式按照指定的年月日时分秒来构造对象
        // 2.1 获取Calendar类型的引用
        // 考点:既然 Calendar 是个抽象类不能创建对象,那么下面的方法为啥可以获取Calendar类型的引用呢?
        // 解析:由源码可知,返回的并不是 Calendar 类型的对象,而是 Calendar 类的子类  GregorianCalendar 等对象,形成了多态
        // 多态的使用场合之三
        Calendar instance = Calendar.getInstance();
        // 2.2 设置指定的年月日时分秒信息
        instance.set(2008, 8 - 1, 8, 20, 8, 8);
        // 2.3 转换为Date类型的对象
        Date d2 = instance.getTime();
        String format1 = sdf.format(d2);
        System.out.println("获取到的时间是:" + format1); // 2008-08-08 20:08:08

        System.out.println("----------------------------------------------------------");
        // 3.向指定的字段设置以及增加指定的数值
        instance.set(Calendar.YEAR, 2018);
        // 转换为Date类型并按照指定的格式打印
        Date d3 = instance.getTime();
        System.out.println("设置年份后的结果是:" + sdf.format(d3)); // 2018-08-08 20:08:08

        instance.add(Calendar.MONDAY, 2);
        Date d4 = instance.getTime();
        System.out.println("增加月份后的结果是:" + sdf.format(d4)); // 2018-10-08 20:08:08
    }
}

多态的使用场合

public static void draw(Shape s){ 
     s.show();
}
draw(new Rect(1, 2, 3, 4));
Account acc = new FixedAccount();
Calender getInstance(){
    return new GregorianCalendar(zone, aLocale);
}

Java8中的日期相关类(熟悉)

Java8日期类的由来

Java8日期类的概述

LocalDate类的概述

基本概念

常用的方法

方法声明 功能介绍
static LocalDate now() 在默认时区中从系统时钟获取当前日期

LocalTime类的概述

基本概念

常用的方法

方法声明 功能介绍
static LocalTime now() 从默认时区的系统时间中获取当前时间
static LocalTime now(ZoneId zone) 获取指定时区的当前时间

LocalDateTime类的概述

基本概念

常用的方法

方法声明 功能介绍
static LocalDateTime now() 从默认时区的系统时间中获取当前日期时间
static LocalDateTime of(int year, int month, intdayOfMonth, int hour, int minute, int second) 根据参数指定的年月日时分秒信息来设置日期时间
int getYear() 获取年份字段的数值
int getMonthValue() 获取1到12之间的月份字段
int getDayOfMonth() 获取日期字段
int getHour() 获取小时数
int getMinute() 获取分钟数
int getSecond() 获取秒数
LocalDateTime withYear(int year) 设置为参数指定的年
LocalDateTime withMonth(int month) 设置为参数指定的月
LocalDateTime withDayOfMonth(int dayOfMonth) 设置为参数指定的日
LocalDateTime withHour(int hour) 设置为参数指定的时
LocalDateTime withMinute(int minute) 设置为参数指定的分
LocalDateTime withSecond(int second) 设置为参数指定的秒
LocalDateTime plusYears(long years) 加上参数指定的年
LocalDateTime plusMonths(long months) 加上参数指定的月
LocalDateTime plusDays(long days) 加上参数指定的日
LocalDateTime plusHours(long hours) 加上参数指定的时
LocalDateTime plusMinutes(long minutes) 加上参数指定的分
LocalDateTime plusSeconds(long seconds) 加上参数指定的秒
LocalDateTime minusYears(long years) 减去参数指定的年
LocalDateTime minusMonths(long months) 减去参数指定的月
LocalDateTime minusDays(long days) 减去参数指定的日
LocalDateTime minusHours(long hours) 减去参数指定的时
LocalDateTime minusMinutes(long minutes) 减去参数指定的分
LocalDateTime minusSeconds(long seconds) 减去参数指定的秒
package com.lagou.task13;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class LocalDateTimeTest {
    public static void main(String[] args) {
        // 1.获取当前日期信息并打印
        LocalDate now = LocalDate.now();
        System.out.println("获取到的当前日期是:" + now); // 2021-01-03
        // 2.获取当前时间信息并打印
        LocalTime now1 = LocalTime.now();
        System.out.println("获取到的当前时间是:" + now1); // 23:02:20.991097100
        // 3.获取当前日期时间信息并打印,使用最多
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println("获取到的当前日期时间是:" + now2); // 2021-01-03T23:02:20.992095300

        System.out.println("---------------------------------------------------");
        // 4.使用参数指定的年月日时分秒信息来获取对象并打印
        LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 8, 8);
        System.out.println("指定的日期是:" + of); // 2008-08-08T20:08:08  自动调用toString方法
        System.out.println("获取到的年是:" + of.getYear()); // 2008
        System.out.println("获取到的月是:" + of.getMonthValue()); // 8
        System.out.println("获取到的日是:" + of.getDayOfMonth()); // 8
        System.out.println("获取到的时是:" + of.getHour()); // 20
        System.out.println("获取到的分是:" + of.getMinute()); // 8
        System.out.println("获取到的秒是:" + of.getSecond()); // 8

        System.out.println("---------------------------------------------------");
        // 5.实现特征的设置并打印
        // 与 String 类型相似,调用对象本身的数据内容不会改变,返回值相当于创建了一个新的对象,由此证明了不可变性
        LocalDateTime localDateTime = of.withYear(2012);
        System.out.println("localDateTime = " + localDateTime); // 2012-08-08T20:08:08
        System.out.println("of = " + of); // 2008-08-08T20:08:08
        LocalDateTime localDateTime1 = localDateTime.withMonth(12);
        System.out.println("localDateTime1 = " + localDateTime1); // 2012-12-08T20:08:08

        System.out.println("---------------------------------------------------");
        // 6.实现特征的增加并打印
        LocalDateTime localDateTime2 = localDateTime1.plusDays(2);
        System.out.println("localDateTime2 = " + localDateTime2); // 2012-12-10T20:08:08
        System.out.println("localDateTime1 = " + localDateTime1); // 2012-12-08T20:08:08

        LocalDateTime localDateTime3 = localDateTime2.plusHours(3);
        System.out.println("localDateTime3 = " + localDateTime3); // 2012-12-10T23:08:08

        System.out.println("---------------------------------------------------");
        // 7.实现特征的减少并打印
        LocalDateTime localDateTime4 = localDateTime3.minusMinutes(1);
        System.out.println("localDateTime4 = " + localDateTime4); // 2012-12-10T23:07:08
        System.out.println("localDateTime3 = " + localDateTime3); // 2012-12-10T23:08:08

        LocalDateTime localDateTime5 = localDateTime4.minusSeconds(3);
        System.out.println("localDateTime5 = " + localDateTime5); // 2012-12-10T23:07:05
    }
}

Instant类的概述

基本概念

常用的方法

方法声明 功能介绍
static Instant now() 从系统时钟上获取当前时间
OffsetDateTime atOffset(ZoneOffset offset) 将此瞬间与偏移量组合以创建偏移日期时间
static Instant ofEpochMilli(longepochMilli) 根据参数指定的毫秒数来构造对象,参数为距离1970年1月1日0时0分0秒的毫秒数
long toEpochMilli() 获取距离1970年1月1日0时0分0秒的毫秒数
package com.lagou.task13;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class InstantTest {
    public static void main(String[] args) {
        // 1.设置Instant类来获取当前系统时间 并不是当前系统的默认时区 本初子午线 差8小时
        Instant now = Instant.now();
        System.out.println("获取到的当前时间为:" + now); // 2021-01-03T15:27:41.448697100Z

        // 2.加上时区所差的8小时
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println("偏移后的日期时间为:" + offsetDateTime); // 2021-01-03T23:27:41.448697100+08:00

        System.out.println("------------------------------------------------------");
        // 3.获取当前调用对象距离标准基准时间的毫秒数
        long g1 = now.toEpochMilli();
        System.out.println("获取到的毫秒差为:" + g1); // 1609687783934

        // 4.根据参数指定的毫秒数来构造对象
        Instant instant = Instant.ofEpochMilli(g1);
        System.out.println("根据参数指定的毫秒数构造出来的对象为:" + instant); // 2021-01-03T15:31:49.853Z
    }
}

DateTimeFormatter类的概述

基本概念

常用的方法

方法声明 功能介绍
static DateTimeFormatter ofPattern(String pattern) 根据参数指定的模式来获取对象
String format(TemporalAccessor temporal) 将参数指定日期时间转换为字符串
TemporalAccessor parse(CharSequence text) 将参数指定字符串转换为日期时间
package com.lagou.task13;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class DateTimeFormatterTest {
    public static void main(String[] args) {
        // 1.获取当前系统的日期时间并打印
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);// 2021-01-04T06:34:49.170680200
        // 2.按照指定的格式准备一个DateTimeFormatter类型的对象
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 3.实现日期时间向字符串类型的转换并打印
        String format = dateTimeFormatter.format(now);
        System.out.println("调整格式后的结果是:" + format); // 2021-01-04 06:34:49
        // 4.实现字符串类型到日期时间类型的转换并打印
        TemporalAccessor parse = dateTimeFormatter.parse(format);
        System.out.println("转回去的结果是:" + parse); // {},ISO resolved to 2021-01-04T06:34:49
    }
}
上一篇 下一篇

猜你喜欢

热点阅读