新增日期类、包装类、正则表达式、Arrays、Lambda表达式(java基础进阶)

Date类概述

Date类的对象在Java中代表的是当前所在系统的此刻日期时间

Date构造器

名称说明
public Date()创建一个Date对象,代表的是系统当前此刻日期时间

方法

名称说明
public long getTime()获取时间对象的毫秒值

时间毫秒值->日期对象

构造器说明
public Date(long time)把毫秒值转换成Data日期对象
Data方法说明
public void setTime(long time)设置日期对象的时间为当前时间毫秒值对应的时间

SimpleDateFormat类

作用:可以对Date对象或时间毫秒值格式化成我们喜欢的时间形式
也可以把字符串的时间形式解析成日期对象

SimpleDateFormat的构造器

构造器说明
public SimpleDateFormat()构造一个SimpleDataFormat,使用默认格式
public SimpleDateFormat(String pattern)构造一个SimpleDataFormat,使用指定的格式

SimpleDateFormat的格式方法

格式化方法说明
public final String format(Date date)将日期格式化成日期/时间字符串
public final String format(Object time)将时间毫秒数格式成日期/时间字符串

解析字符串时间成为日期对象

解析方法说明
public Date parse(String source)从给定字符串的开始解析文本以生成日期

Calendar概述

Calendar代表了系统此刻日期对应的日历对象
Calendar是一个抽象类,不能直接创建对象

日历类创建日历对象的方法

方法名说明
public static CalendargetInstance()获取当前日历对象

常用方法

方法名说明
public int get(int field)取日期中的某个字段信息
public void set(int field,int value)修改日历的某个字段信息
public void add(int field,int amount)为某个字段增加/减少指定的值
public final Date getTime()拿到此刻日期对象
public long getTimeInMillis()拿到此刻日期毫秒值

注意:calendar是可变日期对象,一旦修改后其对象本身表示的时间将产生变化

JDK8新增日期类

在这里插入图片描述

LocalDate、localTime、localDateTime

分别表示日期,时间,日期时间对象,他们的类的实例是不可变的对象
三者构建对象和API都是通用的

构建对象的方式如下

方法名说明
public static Xxxx now();静态方法,根据当前时间创建对象
public static Xxxx of();静态方法,指定日期/时间创建对象

localDateTime的转换API

方法名说明
public localDate toLocalDate()转换成一个LocalDate对象
public localDate toLocalTime()转换成一个LocalTime对象

在这里插入图片描述

Instant时间戳

由一个静态的工厂方法now()可以返回当前时间戳

Instant instant=Instant.now();
System.out.println("当前时间戳是"+instant);
Date date=Date.from(instant);
System.out.println("当前时间戳是"+date);
instant = date.toInstant();
System.out.println(instant);

时间戳是包含日期和时间的,与java.util.Date很类似
Instant和Date这两个类可以进行转换

DateTimeFormatter

在JDK8中,引入一个全新的日期与时间格式器
正反调用format方法

        //本地此刻 日期时间 对象
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        // 解析/格式化器   2022-6-29 15:44:12 周三 下午
 DateTimeFormatter dtf
  = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EEE a");
        // 正向格式化
        System.out.println(dtf.format(ldt));
        //逆向格式化
        System.out.println(ldt.format(dtf));

        //解析字符串时间
  DateTimeFormatter dtf1
   = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateStr="2020-11-11 11:11:11";
        //解析当前时间对象成为本地日期时间对象
    LocalDateTime ldt1 = LocalDateTime.parse(dateStr, dtf1);
        System.out.println(ldt1);

Duration和Period类

Period类
可以使用以下类来计日期间隔差异:java.time.period
主要是Period类方法 getYears()、getMonths()和getDays()来计算,只能精确到年月日
用于LocalDate之间的比较

        //当前本地 年月日
        LocalDate today = LocalDate.now();
        System.out.println(today);

        //生日的 年月日
        LocalDate birthday = LocalDate.of(2000, 11, 01);
        System.out.println(birthday);

        Period period = Period.between(birthday, today);

        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());

Duration类
可以使用以下类计算时间间隔差异:java.time.Duration
提供了使用基于时间的值测量时间量的方法
用于LocalDateTime之间的比较。也可用于Instant之间的比较

        //本地日期时间对象
        LocalDateTime today = LocalDateTime.now();
        System.out.println(today);

        LocalDateTime birthday = LocalDateTime.of(2021, 03
                , 14, 20, 00, 00);

        Duration duration = Duration.between(birthday, today);

        System.out.println(duration.toDays());//两个时间差的天数
        System.out.println(duration.toHours());//两个时间差的小时数
        System.out.println(duration.toMinutes());//两个时间差的分钟数
        System.out.println(duration.toMillis());//两个时间差的毫秒数
        System.out.println(duration.toNanos());//两个时间差的纳秒数

ChronoUnit

可以在单个时间单位内测量一段时间,这个工具类很全,可以用于比较所有的时间单位
会查API文档就行

包装类
其实就是8种基本数据类型对应的引用类型

基本数据类型引用数据类型
byteByte
shortShort
intInteger
longLong
charCharacter
floatFloat
doubleDouble
booleanBoolean

为什么提供包装类
java为了实现一切皆对象,为8种基本数据类型提供了对应的引用类型
后面的集合和泛型其实也只能支持包装类型。不支持基本数据类型

自动装箱:基本类型的数据和变量可以直接赋值给包装类型的变量
自动拆箱:包装类型的变量可以直接赋值给基本数据类型的变量
包装类的特有功能
包装类的变量的默认值可以是null,容错率高

可以把基本类型的数据转换成字符串类型(用处不大)
调用String()方法得到字符串的结果
调用Integer.toString(基本类型的数据)
可以把字符串类型的数值转换成真实的数据类型(真的很有用)
Integer.parseInt(“字符串类型的整数”)
Double.parseDouble(“字符串类型的小数”)

// valueOf()方法是很常用
String num="12";
int age = Integer.valueOf(num);
System.out.println(age + 1);//13

正则表达式

可以用一些规定的字符来制定规则,并用来校验数据格式的合法性

初体验

//验证qq号码
    public static boolean checkQQ(String qq) {
        return qq != null && qq.matches("\\d{6,20}");
    }

匹配规则
在这里插入图片描述

正则表达式在字符串方法中的使用

方法名说明
public String replaceAll(String regex, String newStr)按照正则表达式匹配的内容进行替换
public String[] spilt(String regex)按照正则表达式匹配的内容进行分割字符串,返回一个字符串数组

支持爬取信息
在这里插入图片描述

Arrays类概述

数组操作工具类,专门用于操作数组元素的

Arrays的常用API

方法名说明
public static String toString(类型[] a)返回数组的内容
public static void sort(类型[] a)对数组进行默认升序排序
public static void sort(类型[] a,Comparator<?super T> c)使用比较器对象自定义排序
public static int binarySearch(int[] a,int key)二分搜索数组中的数据,存在返回索引,否则返回-1

自定义比较规则
在这里插入图片描述

选择排序

每轮选择当前位置,开始找出后面较小值与该位置交换

    public static String selectSort(int[] arr) {
        int temp = 0;
        //定义一个循环控制需要选择几轮
        for (int i = 0; i < arr.length - 1; i++) {
            //定义里层循环,控制选择几次
            for (int j = i + 1; j < arr.length; j++) {
                //当前位 arr[j]
                //如果有比当前位数据更小的,则交换
                if (arr[i] > arr[j]) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return Arrays.toString(arr);
    }

二分查找

前提是必须排好序的数据

public static int binarySearch(int[] arr, int data) {
        //定义左变量和右变量
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            //定义中间变量
            int middleIndex = (left + right) / 2;

            if (data > arr[middleIndex]) {
                left = middleIndex + 1;
            } else if (data < arr[middleIndex]) {
                right = middleIndex - 1;
            } else {
                return middleIndex;
            }

        }
        return -1;// 查无此元素
    }

结论:二分查找正常的检索条件是开始位置min<=结束位置max

Lambda表达式

概述
JDK8开始后的一种新语法形式
作用:简化匿名内部类的代码写法

//Lambda表达式的简化格式
(匿名内部类被重写方法的形参列表)->{
//被重写方法的方法体代码
}
//注: ->是语法格式,无实际含义

注意:Lambda表达式只能简化函数式接口的匿名内部类的写法形式

什么是函数式接口?
首先必须是接口、其次接口有且仅有一个抽象方法的形式
通常我们会在接口上加上一个@FunctionalInterface注解,标记该接口必须是满足函数式接口

常用Lambda简化形式

        Integer[] ages = {24, 65, 45, 75};

//        Arrays.sort(ages, new Comparator<Integer>() {
//            @Override
//            public int compare(Integer o1, Integer o2) {
//                return o1 - o2;//升序
//            }
//        });
        Arrays.sort(ages,(Integer o1, Integer o2)-> {
                return o1 - o2;//升序
            });

        System.out.println(Arrays.toString(ages));

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值