Java常用类

Java常用类

Java提供了丰富的基础类库,可以提高效率,降低开发难度,主要有String类与StringBuffer类,System类与Runtime类,Math类与Random类,包装类,日期与时间类,格式化类

String类

  • 字符串:是指一连串的字符,它有许多单个字符连接而成。
  • 字符串可以包含任意字符,这些字符必须包含在意对英文双引号(“”)之内,如"abc"
  • Java中定义了String与StringBuffer两个类来封装字符串,他们位于java.lang包中,不需要导包

String类初始化

  • 使用字符串常量直接初始化一个String对象

  • 使用String的构造方法初始化字符串对象

    方法声明功能描述
    String()创建一个内容为空的字符串
    String(String value)根据指定的字符串内容创建对象
    String(char[] value)根据指定的字符数组创建对象
package top.dty.commonClass;

public class TestString {
    public static void main(String[] args) {
        //String 变量名 = 字符串;
        //初始化为空
        String str1 = null;
        //初始化为空字符串
        String str2 = "";
        //初始化为xiaoduan,其中xiaoduan为字符串常量
        String str3 = "xiaoduan";

        //String 变量名 = new String(字符串);
        //创建一个空字符串
        String str4 = new String();
        //创建一个内容xiaoduan为字符串
        String str5 = new String("xiaoduan");
        //创建一个内容为字符数组的字符串
        char[] chars = {'a', 'b', 'c'};
        String str6 = new String(chars);

        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str4);
        System.out.println(str5);
        System.out.println(str6);
    }
}

String类的常用方法

方法声明功能描述
length()返回字符串长度
charAt(int ch)返回某个位置的字符
indexOf()返回某字符串(字符)第一次出现的位置
lastIndexOf返回某字符串(字符)最一次出现的位置
toCharArray()将字符转换为字符数组
toUpperCase()将字符串转换为大写
trim()去除字符两端的空格
replace(" “,”")将字符替换成另一字符
tartsWith()判断以什么为开头的字符串
endsWith()判断以什么为结尾的字符串
contains()判断以什么为内容的字符串
isEmpty()判断字符串为空
equals()判断字符串是否相等
substring(int ch,int ch)截取某位置的字符串
package top.dty.commonClass;

public class TestString02 {
    public static void main(String[] args) {

        String str1 = "abcabccbaddca";
        System.out.println(str1.length());//字符串长度
        System.out.println(str1.charAt(0));//某个位置的字符
        System.out.println(str1.indexOf('c'));//某字符第一次出现的位置
        System.out.println(str1.lastIndexOf('a'));//某字符出现最后一次的位置
        System.out.println(str1.indexOf("ab"));//某字符串第一次出现的位置
        System.out.println(str1.lastIndexOf("ca"));//某字符串出现最后一次的位置
        System.out.println("===================");

        String str2 = new String("java");
        //将字符转换为字符数组
        char[] chars = str2.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(i != chars.length-1){
                System.out.print(chars[i]+",");
            }else {
                System.out.println(chars[i]);
            }

        }
        //将int值转换为String类型
        System.out.println(String.valueOf(12));
        //将字符串转换为大写
        System.out.println(str2.toUpperCase());
        System.out.println("===================");

        String str3 = "         http : // localhost : 8080   ";
        System.out.println(str3);
        //去除字符两端的空格
        System.out.println(str3.trim());
        //将字符替换成另一字符
        System.out.println(str3.replace(" ",""));
        System.out.println("===================");

        String str4 = new String("java");
        //判断以什么为开头的字符串
        System.out.println(str4.startsWith("ja"));
        //判断以什么为结尾的字符串
        System.out.println(str4.endsWith("ja"));
        //判断以什么为内容的字符串
        System.out.println(str4.contains("av"));
        //判断字符串为空
        System.out.println(str4.isEmpty());

        //判断字符串是否相等
        System.out.println(str2==str4);//两个不同的对象
        System.out.println(str2.equals(str4));//字符串内容相同
        System.out.println("===================");

        String str5 = "2022-04-21";
        //截取某位置的字符串
        System.out.println(str5.substring(5));
        System.out.println(str5.substring(5,7));
        String[] strings = str5.split("-");
        //用方法将-转化为.
        for (int i = 0; i < strings.length; i++) {
            if(i != strings.length-1){
                System.out.print(strings[i]+".");
            }else {
                System.out.println(strings[i]);
            }
        }
        System.out.println("===================");
    }
}

Stringbuffer类(StringBuilder类)

Stringbuffer与StringBuilder的功能相似,且两个类中提供的方法基本相同。

Stringbuffer是线程安全的,StringBuilder没有线程安全功能,性能略高。

通常情况下,优先考虑StringBuilder类。

  • String类是final类型,所有String定义的字符串是一个常量
  • StringBuffer类(字符串缓冲区)方便对字符串进行修改
  • StringBuffer类和String类最大的区别在于StringBuffer类的内容和长度是可以改变的
  • StringBuffer类似一个字符容器,当前其中添加或删除字符时都是对于这个容器,不会产生新的StringBuffer对象

Stringbuffer类的常用方法

方法声明功能描述
append(String)添加字符串
insert(int,String)在指定位置插入字符串
setCharAt(int,String)修改指定位置的字符
replace(int,int,String)替换指定位置的字符
delete(int,int)删除指定范围的字符串
deleteCharAt(int)删除指定位置的字符
package top.dty.commonClass;

public class TestStringBuffer {
    public static void main(String[] args) {
        TestStringBuffer testStringBuffer = new TestStringBuffer();
        StringBuffer stringBuffer = new StringBuffer("166-");
//        //用StringBuilder创建字符串
//        StringBuilder stringBuilder = new StringBuilder("aini");
//        System.out.println(stringBuilder.append("you"));
        testStringBuffer.add(stringBuffer);
        testStringBuffer.update(stringBuffer);
        testStringBuffer.delete(stringBuffer);
    }
    private void add(StringBuffer stringBuffer){
        System.out.println("add->");
        //添加字符串
        System.out.println(stringBuffer.append("xiao"));
        //在指定位置插入字符串
        System.out.println(stringBuffer.insert(8,"duan"));
    }
    private void update(StringBuffer stringBuffer){
        System.out.println("update->");
        //修改指定位置的字符
        stringBuffer.setCharAt(0,'6');
        System.out.println(stringBuffer);
        //替换指定位置的字符
        System.out.println(stringBuffer.replace(3,4,"."));
    }
    private void delete(StringBuffer stringBuffer){
        System.out.println("delete->");
        //删除指定范围的字符串
        System.out.println(stringBuffer.delete(0,3));
        //删除指定位置的字符
        System.out.println(stringBuffer.deleteCharAt(0));
        //清空缓冲区(就是全部删除)
        System.out.println(stringBuffer.delete(0,stringBuffer.length()));
    }
}

System类

System类所提供的方法都是静态的,想要引用这些属性和方法,直接使用System类调用即可。

System类的常用方法

方法声明功能描述
getProperties()获取当前系统属性
getProperty(String key)获取当前键key(属性名)所对应的值(属性值)
currentTimeMillis()获取系统当前时间
arraycopy()将一个数组元素快速拷贝到另一个数组
gc()运行垃圾回收器,并对垃圾回收
exit(0)终止当前正在运行的java虚拟机
package top.dty.commonClass;

import java.util.Properties;
import java.util.Set;

public class TestSystem {
    public static void main(String[] args) {
        //获取当前系统属性
        Properties properties = System.getProperties();
        System.out.println(properties);
        //获取所有系统属性的key(属性名),返回set对象
        Set<String> strings = properties.stringPropertyNames();
        for (String key : strings) {
            //获取当前键key(属性名)所对应的值(属性值)
            String property = System.getProperty(key);
            System.out.println(key+"--->"+property);
        }
        System.out.println("=================");

         //获取系统当前时间
        long startTime = System.currentTimeMillis();
        int sum = 0;
        for (int i = 0; i < 10000; i++) {
            for (int j = 0; j < 10000; j++) {
                sum+=i+j;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println((endTime-startTime)+"ms");
        System.out.println("=================");

        int[] srcArray = {101,102,103,104,105,106};
        int[] destArray = {201,202,203,204,205};
        //将一个数组元素快速拷贝到另一个数组
        //(原数组,原数组起始拷贝位置,目标数组,目标数组起始拷贝位置,拷贝元素的个数)
        System.arraycopy(srcArray,2,destArray,0,4);
        for (int i = 0; i < destArray.length; i++) {
            System.out.println(i+":"+destArray[i]);
        }
        System.out.println("=================");
    }
}

Runtime类

  • Runtime类用于表示Java虚拟机运行时的状态,用于封装Java虚拟机进程。
  • 每次使用java命令启动Java虚拟机时都会对于一个Runtime实例,并且只有一个实例,应用程序会通过该实例与其运行时的环境相连。
  • getRuntime()方法获得与之相关的Runtime对象
方法声明功能描述
availableProcessors()获取处理器的个数
freeMemory()获取空闲内存
maxMemory()获取最大内存
exec()执行一个DOS命令
exec().destroy()关闭进程
package top.dty.commonClass;

import java.io.IOException;

public class TestRuntime {
    public static void main(String[] args) throws Exception {
        //获得与之相关的Runtime对象
        Runtime runtime = Runtime.getRuntime();
        //获取处理器的个数
        System.out.println(runtime.availableProcessors());
        //获取空闲内存
        System.out.println(runtime.freeMemory()/1024/1024+"M");
        //获取最大内存
        System.out.println(runtime.maxMemory()/1024/1024+"M");

        //执行一个DOS命令
        Process process = runtime.exec("notepad.exe");//打开记事本
        //线程休眠3秒
        Thread.sleep(3000);
        //关闭进程
        process.destroy();
        runtime.exec("notepad.exe").destroy();
    }
}

Math类

  • Math类是一个工具类,主要用于完成复杂的数学运算
  • Math类构方法被定义成private,因此无法创建Math类的对象
  • Math类中所有的方法都是静态方法,可以直接通过类名来调用他们
  • Math类中还定义来两个静态常量PI和E,分别代表数学中的π和e
package top.dty.commonClass;

public class TestMath {
    public static void main(String[] args) {
        //Math类的常用方法
        System.out.println(Math.sin(Math.PI/2));//sin函数,常量PI
        System.out.println(Math.abs(-1));//绝对值
        System.out.println(Math.sqrt(4));//平方根
        System.out.println(Math.cbrt(8));//立方根
        System.out.println(Math.pow(2,2));//乘方的计算
        System.out.println(Math.ceil(4.6));//大于参数的最小整数
        System.out.println(Math.floor(-5.2));//小于参数的最大整数
        System.out.println(Math.round(-8.6));//对小数进行四舍五入
        System.out.println(Math.max(5, 6.6));//求两个数的最大值
        System.out.println(Math.min(2, -0.1));//求两个数的最小值
    }
}

Random类

Random类可以在指定的取值范围内随机产生数字

Random类构造器

方法声明功能描述
Random()用于创建一个随机数生成器,每次实例化Random对象会生成不同的随机数
Random(long seed)是一个long型的seed(种子)创建伪随机数生成器,当seed相同时,每次实例化Random对象会生成相同的随机数

Random类常用的方法

方法声明功能描述
nextBoolean()随机生成boolean类型的随机数
nextDouble()随机生成double类型的随机数
nextFloat()随机生成float类型的随机数
nextInt()随机生成int类型的随机数
nextInt(int n)随机生成[0,n)之间int类型的随机数
nextLong()随机生成long类型的随机数
package top.dty.commonClass;

import java.util.Random;

public class TestRandom {
    public static void main(String[] args) {
        Random random = new Random();
//        Random random = new Random(1);
        System.out.println(random.nextBoolean());//随机生成boolean类型的随机数
        System.out.println(random.nextDouble());//随机生成double类型的随机数
        System.out.println(random.nextFloat());//随机生成float类型的随机数
        System.out.println(random.nextInt());//随机生成int类型的随机数
        System.out.println(random.nextInt(10));//随机生成[0,n)之间int类型的随机数
        System.out.println(random.nextLong());//随机生成long类型的随机数
    }
}

包装类

  • Java中的8中基本数据类型不支持面向对象的编程机制(没有属性和方法)
  • JDK提供一系列的包装类,通过这些包装类可以将数据类型的值包装为引用数据类型的对象

基本类型对象的包装类

  • 除了Character和Integer类外,其他对应的包装类的名称斗鱼基本数据类型一样,只不过首字母需要大写
  • 自动装箱(Autoboxing)是自动将基本数据类型转换为包装器类型
  • 自动拆箱(AutoUnboxing)是自动将包装器类型转换为基本数据类型
基本类型包装器类型
booleanBoolean
charCharacter
intInteger
byteByte
shortShort
longLong
floatFloat
doubleDouble
package top.dty.commonClass;

public class TestPack {
    public static void main(String[] args) {
        //定义一个基本类型的变量a,赋值为2022
        int a = 2022;
        //自动装箱:将基本类型的变量a赋给Integer类型的变量b
        Integer b = a;
        System.out.println(b);
        //自动拆箱:将Integer类型的变量b赋给基本类型的变量c
        int c = b;
        System.out.println(c);
    }
}

相互转换

  • 数据类型String类的value()方法可以将8中基本数据类型转换为对应的字符串类型
  • 包装类的静态方法value()可以将对应的基本数据类型中转换为包装类,也可以将变量内容匹配的字符串转换为对应的包装类(Characterbao包装类除外)
  • 包装类的有参构造方法可以将对应的基本数据类型中转换为包装类,也可以将变量内容匹配的字符串转换为对应的包装类(Characterbao包装类除外)
  • 包装类的静态方法parseXxx()可以将变量内容匹配的字符串转换为对应的基本数据类型
  • 包装类都重写了Object类中的toString()方法,以字符串的形式返回被包装的基本数据类型的值
package top.dty.commonClass;

public class TestPackClass02 {
    public static void main(String[] args) {
        int num = 2022;
        //通过String.valueOf()方法将基本类型转换为字符串
        String string = String.valueOf(num);
        System.out.println(string);
        String str = "04";
        //通过包装类的valueOf()静态方法将基本类型和字符串转换为包装类
        Integer integer1 = Integer.valueOf(num);
        Integer integer2 = Integer.valueOf(str);
        System.out.println(integer1);
        System.out.println(integer2);
        通过包装类的有参构造器将基本类型和字符串转换为包装类
        Integer integer3 = new Integer(num);
        Integer integer4 = new Integer(str);
        System.out.println(integer3);
        System.out.println(integer4);
        //通过包装类的parseXxx()静态方法将字符串变量转换为基本数据类型
        int parseInt = Integer.parseInt(str);
        System.out.println(parseInt);
        //通过包装类的toString()方法将包装类转换为字符串
        String string1 = integer1.toString();
        System.out.println(string1 );
    }
}

时间与日期类

Date类

在JDK的java.util包中提供了一个Date类用于表示日期和时间(不推荐使用)

构造器

  • Date():用来创建当前日期时间的Date对象
  • Date(long date):用于创建指定时间的Date对象,其中date参数表示时间戳
package top.dty.annotation;

import java.util.Date;

public class TestDate {
    public static void main(String[] args) throws InterruptedException {
        //创建表示当前日期的Date对象
        Date date = new Date();
        System.out.println(date);
        //使线程沉睡3秒
        Thread.sleep(3_000);
        Date date1 = new Date();
        System.out.println(date1);
        //获取当前时间后1秒的时间
        Date date2 = new Date(System.currentTimeMillis() + 1000);
        System.out.println(date2);
    }
}

Calendar类

  • Calendar类用于完成日期和时间字段的操作,特可以通过特定的方法设置和读取日期的特定部分
  • Calendar类是一个抽象类,不可被实例化,在程序中需要调用其静态方法getInstance()来得到一个Calend类对象
  • setLenient()用来设置日历的容错模式,当为ture时开,false关闭

Calendar的常用方法

方法声明功能描述
get()返回指定日历字段的值
add()根据日历规则,为指定的日历字段增加时间
set()设置日历字段的值
setLenient()用来设置日历的容错模式,当为ture时开,false关闭
getTime()返回一个表示Calendar时间值的Date对象
SetTime()将Date对象r时间值一设置给Calendar对象
package top.dty.annotation;

import java.util.Calendar;

public class TestCalendar {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        //使用Calendar.MONTH字段时,月份的起始值为0开始,要获取当前的月份要+1
        int month = calendar.get(Calendar.MONTH)+1;
        int date = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        System.out.println(year+"."+month+"."+date+" "+hour+":"+minute+":"+second);

        calendar.set(2022,04,23);
        calendar.add(Calendar.DATE,100);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH)+1;
        date = calendar.get(Calendar.DATE);
        System.out.println(year+"."+month+"."+date);
        //setLenient()用来设置日历的容错模式,当为ture时开,false关闭
//        calendar.setLenient(false);
        calendar.set(Calendar.MONTH,13);
        System.out.println(calendar.getTime());
    }
}

JDK8的日期与时间类

通过clock.instant()和Instant.now()获取的当前时间与本地系统显示时间有8小时的时差,Instant默认使用UTC(Universal Time Coordinated)协调世界时间,又称时间标准时间

常用类

方法声明功能描述
Clock()用于获取指定时区的当前日期,时间
Duration()表示持续时间
Instant()表示一个具体时间,可以精确到纳秒
LocalDate()表示不带时区的日期
LocalDateTime()表示不带时区的日期,时间
Year(),YearMonth(),MonthDay()返回当前的年,月,日
ZoneId()表示一个时区
ZonedDateTime()表示一个时区化的日期,时间
package top.dty.annotation;

import java.time.*;

public class TestJDk8 {
    public static void main(String[] args) {
        //Clock用于获取指定时区的当前日期,时间
        Clock clock = Clock.systemUTC();
        System.out.println(clock.instant());
        System.out.println(clock.millis());
        System.out.println("==============");
        //Duration表示持续时间
        Duration duration = Duration.ofDays(1);
        System.out.println(duration.toHours());
        System.out.println(duration.toMinutes());
        System.out.println(duration.toMillis());
        System.out.println("==============");
        //Instant表示一个具体时间,可以精确到纳秒
        Instant instant = Instant.now();
        System.out.println(instant);
        System.out.println(instant.plusSeconds(3600));
        System.out.println(instant.minusSeconds(3600));
        System.out.println("==============");
        //LocalDate表示不带时区的日期
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        System.out.println("==============");
        //LocalDateTime表示不带时区的日期,时间
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        System.out.println(localDateTime.plusDays(1).plusHours(2).plusMinutes(3));
        System.out.println("==============");
        //Year,YearMonth,MonthDay
        Year year = Year.now();
        System.out.println(year);
        YearMonth yearMonth = YearMonth.now();
        System.out.println(yearMonth);
        MonthDay monthDay = MonthDay.now();
        System.out.println(monthDay);
        System.out.println("==============");
        //ZoneId表示一个时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);
        //ZonedDateTime表示一个时区化的日期,时间
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);
    }
}

格式化类

DateFormat类

  • 使用Date类时,在程序打印Date对象所输出的当前时间都是默认的英语格式输出
  • 输出中文格式的时间,就需要用到DateFormat类DateFormat类
  • DateFormat类专门用于将日期格式化为字符串或者将用特定格式显示的日期字符串转换成一个Date对象
  • DateFormat是一个抽象类,不能被直接实例化
  • DateFormat类定义了许多常量
    • FULL常量用于表示完整格式
    • LONG常量用于表示长格式
    • MEDIUM常量用于表示普通格式
    • SHORT常量用于表示短格式
方法声明功能描述
getDateInstance()获得的实例对象用于对日期部分进行格式化
getDateTimeInstance()获得的实例对象用于对日期和时间部分进行格式化
format()将一个Date格式化为日期/时间字符串
parse()将对应格式的字符串解析成Date对象
package top.dty.annotation;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

public class TestDateFormat {
    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        //FULL常量用于表示完整格式,LONG常量用于表示长格式,MEDIUM常量用于表示普通格式,SHORT常量用于表示短格式
        //getDateInstance()方法获得的实例对象用于对日期部分进行格式化
        DateFormat fullFormat = DateFormat.getDateInstance(DateFormat.FULL);
        DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);
        //getDateTimeInstance()方法获得的实例对象用于对日期和时间部分进行格式化
        DateFormat mediumFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);
        DateFormat shortFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
        //将一个Date格式化为日期/时间字符串
        System.out.println(fullFormat.format(date));
        System.out.println(longFormat.format(date));
        System.out.println(mediumFormat.format(date));
        System.out.println(shortFormat.format(date));

        DateFormat dateInstance = DateFormat.getDateInstance(DateFormat.SHORT);
        String str1 = "2022/04/23";
        String str2 = "2022年04月23日";
        System.out.println(dateInstance.parse(str1));
        System.out.println(longFormat.parse(str2));
    }
}

SimpleDateFormat类

  • SimpleDateFormat类是DateFormat类的子类,它可以使用new关键字创建实例对象
  • SimpleDateFormat类构造方法需要接受一个表示日期格式模板的字符串参数
  • 在创建SimpleDateFormat对象时,只要传入合适的格式字符串参数,久能解析各种形式的日期字符串或者将Date日期格式化成任何形式的字符串
  • 格式化字符串是一个日期/时间字段占位符的日期模板
package top.dty.annotation;

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

public class TestSimpleDateFormat {
    public static void main(String[] args) throws ParseException {
        //yyyy年 MM月 dd日 第D天 E星期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 yyyy年的第D天,E");
        System.out.println(simpleDateFormat.format(new Date()));

        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy/MM/dd");
        String str ="2022/04/23";
        System.out.println(simpleDateFormat1.parse(str));
    }
}

DateTimeFormatter类

DateTimeFormatter类可以将日期,时间对象格式化字符串,还能将特定格式的字符串解析成日期,时间对象

获取DateTimeFormatter对象

  • 使用静态常量DateTimeFormatter格式器
  • 使用不同风格的枚举数来创建DateTimeFormatter格式器
  • 根据模式字符串创建DateTimeFormatter格式器

使用DateTimeFormatter的format(TemporalAccessor temporal)方法执行格式化,其中参数temporal是一个TemporalAccessor类型接口,其主要实现类有LocalDate、LocalDateTime

调用LocalDate、LocalDateTime等日期、时间对象的format(DateTimeFormatter formatter)方法执行格式化

DateTimeFormatter解析字符串可以通过日期时间对象所提供的parse(CharSequence text, DateTimeFormatter formatter)方法来实现

package top.dty.annotation;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class TestDateTimeFormatter {
    public static void main(String[] args) {
        //使用常量创建DateTimeFormatter
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;
        System.out.println(isoDateTime.format(localDateTime));
        //使用MEDIUM类型风格的DateTimeFormatter
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        System.out.println(dateTimeFormatter1.format(localDateTime));
        //根据模式字符串来创建DateTimeFormatter格式器
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(dateTimeFormatter2.format(localDateTime));
        System.out.println("================");
        //使用LocalDateTime的arse()方法来执行解析
        String str1 = "2022-04-24 12:12:12";
        String str2 = "2022年04月24日12时12分12秒";
        DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒");
        LocalDateTime localDateTime1 = LocalDateTime.parse(str1, dateTimeFormatter3);
        LocalDateTime localDateTime2 = LocalDateTime.parse(str2, dateTimeFormatter4);
        System.out.println(localDateTime1);
        System.out.println(localDateTime2);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值