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

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

可变字符串类(重点)

基本概念

  • 由于String类描述的字符串内容是个常量不可改变,当需要在Java代码中描述大量类似的字符串时,只能单独申请和存储,此时会造成内存空间的浪费。
  • 为了解决上述问题,可以使用java.lang.StringBuilder类和java.lang.StringBuffer类来描述字符序列可以改变的字符串,如:“ab”。
  • StringBuffer类是从jdk1.0开始存在,属于线程安全的类,因此效率比较低。
  • StringBuilder类是从jdk1.5开始存在,属于非线程安全的类,效率比较高。

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
    }
}
  • 扩容算法源码

  • 注意

    • 作为参数传递的话,方法内部String不会改变其值,StringBuffer和StringBuilder会改变其值。
  • 笔试考点

    • 考点一:既然StringBuilder类的对象本身可以修改,那么为什么成员方法还有返回值呢?
      • 解析:为了连续调用
      • sb3.reverse().append(“1”).append(“2”).insert(0, “3”).delete(0, 1).reverse();
    • 考点二:如何实现StringBuilder类型和String类型之间转换呢?
      • String str3 = sb3.toString();
      • StringBuilder sb5 = new StringBuilder(str3);
    • 考点三:String、StringBuilder、StringBuffer之间效率谁高?排列如何?
      • String < StringBuffer < StringBuilder
  • 下面关于 StringBuilder 类代码的执行结果是()

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;
    }
}
  • StringBuilder 类型是个引用数据类型,所以该类型的变量中记录的是堆区对象的地址信息,调用 show 方法时只是改变了形参变量 y 的指向而不是指向的内容,因此实参变量sb2 指向的内容不会发生改变。
  • 内存结构分析:

返回值的设计

  • StringBuilder的很多方法的返回值均为StringBuilder类型。这些方法的返回语句均为:returnthis。
  • 由此可见,这些方法在对StringBuilder所封装的字符序列进行改变后又返回了该对象的引用。基于这样设计的目的在于可以连续调用。

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

System类的概述

基本概念
  • Java.lang.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类的概述

基本概念
  • java.util.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类的概述

基本概念
  • java.text.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类的概述

基本概念
  • java.util.Calender类主要用于描述特定的瞬间,取代Date类中的过时方法实现全球化。
  • 该类是个抽象类,因此不能实例化对象,其具体子类针对不同国家的日历系统,其中应用最广泛的是GregorianCalendar(格里高利历),对应世界上绝大多数国家/地区使用的标准日历系统。
常用的方法
方法声明功能介绍
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日期类的由来

  • JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了。而Calendar并不比Date好多少。它们面临的问题是:
  • Date类中的年份是从1900开始的,而月份都从0开始。
  • 格式化只对Date类有用,对Calendar类则不能使用。
  • 非线程安全等。

Java8日期类的概述

  • Java 8通过发布新的Date-Time API来进一步加强对 日期与时间的处理。
  • java.time包:该包日期/时间API的基础包。
  • java.time.chrono包:该包提供对不同日历系统的访问。
  • java.time.format包:该包能够格式化和解析日期时间对象。
  • java.time.temporal包:该包包含底层框架和扩展特性。
  • java.time.zone包:该包支持不同时区以及相关规则的类。

LocalDate类的概述

基本概念
  • java.time.LocalDate类主要用于描述年-月-日格式的日期信息,该类不表示时间和时区信息。
常用的方法
方法声明功能介绍
static LocalDate now()在默认时区中从系统时钟获取当前日期

LocalTime类的概述

基本概念
  • java.time.LocalTime 类主要用于描述时间信息,可以描述时分秒以及纳秒。
常用的方法
方法声明功能介绍
static LocalTime now()从默认时区的系统时间中获取当前时间
static LocalTime now(ZoneId zone)获取指定时区的当前时间

LocalDateTime类的概述

基本概念
  • java.time.LocalDateTime类主要用于描述ISO-8601日历系统中没有时区的日期时间,如2007-12-03T10:15:30。
常用的方法
方法声明功能介绍
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类的概述

基本概念
  • java.time.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类的概述

基本概念
  • java.time.format.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
    }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值