Day08-java API下

三、Math 类和Random 类

3.1 Math 类

Math 类提供了大量的静态方法一边人们实现数学运算

方法功能
abs ()该方法用于计算绝对值
sqrt ()该方法用于计算开平方
ceil (a ,b)该方法用于计算大于参数的最小整数
floor ()该方法用于计算小于参数的最小整数
round ()该方法用于计算小鼠进行四舍五入后的结果
max ()该方法用于计算两个数的最大值
min ()该方法用于计算两个数的最小值
random ()该方法用于生成一个大于0.0小于1.0的随机值
pow ()该方法用于计算指数函数的值
public class Demo01 {
    public static void main(String[] args) {
        System.out.println("求绝对值:" + Math.abs(-10));

        System.out.println("求大于参数的最小参数:" + Math.ceil(8.7));
        System.out.println("求小于参数的最大参数:" + Math.floor(-10.5));

        System.out.println("求对小数进行四舍五入的结果:" + Math.abs(8.4));

        System.out.println("求两个数的最大值:" + Math.max(-10, 8));
        System.out.println("求两个数的最小值:" + Math.min(-10, 8));

        System.out.println("求开平方:" + Math.sqrt(9));
        System.out.println("求指数函数的值:" + Math.pow(3, 5));
    }
}

3.2 Random 类

java.util 包中有一个 Random 类,它可以在指定的取值范围内随机产生数字

构造方法功能
Random ()构造方法,用于创建一个伪随机数生成器
Random (long seed)构造方法,使用一个long型的seed创建伪随机数生成器
public class Demo02 {
    public static void main(String[] args) {
        Random r = new Random();
        for(int i = 0; i < 10; i++){
            System.out.print(r.nextInt(50) + " ");
        }
    }
}
方法功能
double nextDouble ()生成 double 类型的随机数
float nextFloat ()生成 float 类型的随机数
int nextInt ()生成 int 类型的随机数
int nextInt (int n)生成 0~n int 类型的随机数
public class Demo03 {
    public static void main(String[] args) {
        Random r = new Random();
        System.out.print("float型随机数:");
        for(int i = 0; i < 10; i++){
            System.out.print(r.nextFloat(50) + " ");
        }
        System.out.print('\n');

        System.out.print("double型随机数:");
        for(int i = 0; i < 10; i++){
            System.out.print(r.nextDouble(50) + " ");
        }
        System.out.print('\n');

        System.out.print("int型随机数:");
        for(int i = 0; i < 10; i++){
            System.out.print(r.nextInt(50) + " ");
        }
        System.out.print('\n');
    }
}

四、日期时间类

类名功能
Instant表示时刻,代表的是时间戳
LocalDate不包含具体时间的日期
LocalTime不包含日期的时间
LocalDateTime包含了日期和时间
Duration基于时间的值测量时间
Period计算日期时间差异,只能精确到年月日
Clock时钟系统,用于查找当前时刻

4.1 Instant类

Instant类代表的是某个时间,其内部由两个Long字段组成,第一部分保存的是(1970年1月1日)到现在的秒数,第二部分保存的是纳秒数

方法功能
now ()从系统时钟获取当前时刻
now (Clock clock)从指定时钟获取当前时刻
ofEpochSecond (long epochSecond)从(1970年1月1日)开始的秒数获得一个Instant的实例
ofEpochMilli (long epochMilli)从(1970年1月1日)开始的毫秒数获得一个Instant的实例
getEpochSecond ()从(1970年1月1日)获取秒数
getNano ()从第二秒开始表示的时间线中返回纳秒数
parse (CharSequence text)从一个文本字符串(如2xxx-xx-xxT 10:15:30.00Z)获取一个Instant实例
from (TemporalAccessor temporal)从时间对象获取一个Instant实例
public class Demo04 {
    public static void main(String[] args) {
        // 时间戳类从1970 - 01 - 01 00:00:00 截止到当前时间的毫秒值
        Instant now = Instant.now();
        System.out.println("从系统获取的当前时刻为:" + now);

        Instant instant = Instant.ofEpochMilli(1000 * 60 * 60 * 24);
        System.out.println("计算机元年增加毫秒数后为:" + instant);

        Instant instant1 = Instant.ofEpochSecond(60 * 60 * 24);
        System.out.println("计算机元年增加秒数后为:" + instant1);
        System.out.println("获取的秒值为:" + Instant.parse("2007-12-03T10:15:30.44Z").getEpochSecond());
        System.out.println("从时间对象获取的纳秒值为:" + Instant.parse("2007-12-03T10:15:30.44Z").getNano());
        System.out.println("从时间对象获取的Instant实例为:" + Instant.from(now));
    }
}

4.2 LocalDate类

LocalDate类仅用来表示日期,通常表示年份以及月份,该类不能代表时间线上得即时信息

// 根据输入年月日获取日期对象
LocalDate date = LocalDate.of(2023, 7, 24);
// 借助当前时间获取日期对象
LocalDate now = LocalDateof.now();
方法功能方法功能
getYear ()获取年份字段parse (CharSequence text, DateTimeFormatter formatter)使用特定格式化LocalDate从文本字符串获取LocalDate的实例
getMonth ()使用Month枚举获取月份字段plusYears (long yearsToAdd)增加指定年份
getMonthValue ()获取月份字段,从1~12plusMonths (long monthsToAdd)增加指定月份
getDayOfMonth ()获取当月第几天字段plusDays (long daysToAdd)增加指定天数
for (DateTimeFormatter formatter)使用指定的格式化程序格式化此日期minusYears (long yearsToSubtract)减少置顶年份
isBefore (ChronoLocalDate other)检查此日期是否在指定日期之前minusMonths (long monthsToSubtract)减少指定月份
isAfter (ChronoLocalDate other)检查此日期是否在指定日期之后minusDays (long daysToSubtract)减少指定日数
isEqual (ChronoLocalDate other)检查此日期是否等于指定的日期withYear (int year)指定年
isLeapYear ()检查此年份是否为闰年withMonth (int month)指定月
parse (CharSequence text)从文本字符串中获取一个LocalDate的实例withDayOfYear (int dayofyear)指定日
public class Demo05 {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        LocalDate of = LocalDate.of(2018, 12 ,10);
        System.out.println("1、LocalDate的获取及格式化的相关方法");
        System.out.println("从LocalDate实例获取的年份为:" + now.getYear());
        System.out.println("从LocalDate实例获取的月份为:" + now.getMonthValue());
        System.out.println("从LocalDate实例获取当天在本月的第几天:" + now.getDayOfMonth());
        System.out.println("将获取到的LocalDate实例格式化为:" + now.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
        System.out.println();

        System.out.println("2、LocalDate判断的相关方法");
        System.out.println("判断日期of是否在now之前:" + of.isBefore(now));
        System.out.println("判断日期of是否在now之后:" + of.isAfter(now));
        System.out.println("判断日期of和now是否相等:" + now.equals(of));
        System.out.println("判断日期of是否是闰年:" + of.isLeapYear());
        System.out.println();

        System.out.println("3、LocalDate解析以及加减操作的相关方法");
        String dateStr = "2020-03-04";
        System.out.println("把日期字符串解析成日期对象为:" + LocalDate.parse(dateStr));
        System.out.println("将LocalDate实例年份加1为:" + now.plusYears(1));
        System.out.println("将LocalDate实例天数减10为:" + now.minusDays(10));
        System.out.println("将LocalDate实例指定年份为2014为:" + now.withYear(2014));
    }
}

4.3 LocalTime 类与 LocalDateTime 类

LocalTime类用来表示时间,通常表示的是小时、分钟、秒

方法与上述LocalDate类类似

public class Demo06 {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        LocalTime of = LocalTime.of(9, 23, 23);
        System.out.println("从LocalTime获取的小时为:" + time.getHour());
        System.out.println("将获取到的LocalTime实例格式化为:" + time.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
        System.out.println("判断时间of是否在now之前:" + of.isBefore(time));
        System.out.println("将时间字符串解析为时间对象后为:" + LocalTime.parse("12:15:30"));
        System.out.println("从LocalTime获取当前时间,不包含毫秒数:" + time.withNano(0));
    }
}

LocalDateTime 类是 LocalDate 类与 LocalTime 类的综合,它既包含日期也包含时间,其中的方法包含了LocalDate类与LocalTime类的方法

public class Demo07 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("获取的当前日期时间为:" + now);
        System.out.println("将目标LocalDateTime转换为相应的LocalDate实例:" + now.toLocalDate());
        System.out.println("将目标LocalDateTime转换为相应的LocalTime实例:" + now.toLocalTime());
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日hh时mm分ss秒");
        System.out.println("格式化后的日期为:" + now.format(ofPattern));
    }
}

4.4 Duration 和 Period 类

1. Duration 类

Duration 类基于时间值,起作用范围是天、时、分、秒、毫秒和纳秒

方法功能
between ( Temporal startInclusive, Temporal end Exclusive )获取一个 Duration 表示两个时间对象之间的持续时间
toDays ()将时间转换为以天为单位
toHours ()将时间转换为以小时为单位
toMinutes ()将时间转换为以分钟为单位
toMillis ()将时间转换为以毫秒为单位
toNanos ()将时间转换为以纳秒为单位
public class Demo08 {
    public static void main(String[] args) {
        LocalTime start = LocalTime.now();
        LocalTime end = LocalTime.of(20, 13, 13);
        Duration duration = Duration.between(start, end);
        System.out.println("时间间隔为:" + duration.toNanos() + "纳秒");
        System.out.println("时间间隔为:" + duration.toMillis() + "毫秒");
        System.out.println("时间间隔为:" + duration.toHours() + "小时");
    }
}

2. Period 类

Period 类主要计算两个日期的间隔,与 Duration 相同,也是通过between计算时间间隔

public class Demo09 {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        LocalDate end = LocalDate.of(2018, 12, 12);
        Period period = Period.between(now, end);
        System.out.println("时间间隔为:" + period.getYears() + "年");
        System.out.println("时间间隔为:" + period.getMonths() + "月");
        System.out.println("时间间隔为:" + period.getDays() + "日");
    }
}

五、包装类

java中的类可以把方法和数据连接在一起,但是java中不能把基本的数据类型作为对象来处理,但某些场合中需要把基本数据类型的数据作为对象来使用

通过包装类,可以把基本数据类型的值包装为引用数据类型的对象

基本数据类型包装类
byteByte
charCharacter
intInteger
shortShort
longLong
floatFloat
doubleDouble
booleanBoolean
public class Demo10 {
    public static void main(String[] args) {
        int a = 20;
        // 装箱
        Integer in = a;
        System.out.println(in);
        // 拆箱
        int l = in;
        System.out.println(l);
    }
}
方法功能
Integer valueOf ( int i )返回一个表示指定的int值的 Integer 实例
Integer valueOf ( String s )返回保存指定的 String 值的 Integer 对象
int parseInt( String s )将字符串参数作为有符号的十进制整数进行解析
intValue ()将Integer 类型的值以 int 类型返回

以Integer为例,介绍包装类特有的方法

public class Demo11 {
    public static void main(String[] args) {
        Integer num = new Integer(30);
        int sum = num.intValue() + 10;
        System.out.println("将Integer类型的值转换为int类型后与10求和为:" + sum);
        System.out.println("返回表示10的Integer实力为:" + Integer.valueOf(10));
    }
}

六、正则表达式

6.1 元字符

正则表达式是由普通字符(字符a-z)和特殊字符(元字符)组成的文字模式。原字符是指那些正则表达式中具有特殊意义的专用字符。

元字符功能元字符功能
\转义字符,例如"\n"匹配"\n"[a-zA-Z]匹配az到AZ
^正则表达式的开头标志[a-z]字符范围,匹配指定范围内的任意字符
$正则表达式的结尾标志\d匹配数字0~9
*匹配零次或多次\D匹配非数字字符
+匹配一次或多次\s匹配空白字符
?匹配一次或零次\S匹配非空白字符
.匹配任意字符\w匹配单词字符与数字0~9
{n}匹配n次\b单词边界
{n,}至少匹配n次\B非单词边界
{n,m}n<=m,最少匹配n次,最多匹配m次\A输入的开头
x|y|匹配 x 或 y\G上一个匹配的结尾
[xyz]字符集合,匹配所包含的任意一个字符\Z输入的结尾,仅用于最后的结束符
[a-z]字符范围,匹配指定范围内的任意字符\z输入的结尾
[^a-z]负值字符范围,匹配任何不在指定范围内的任意字符\b单词边界

6.2 Pattern 类和 Matcher 类

6.2.1 Pattern 类

Pattern类创建一个正则表达式,它的构造方法是私有的,不可以直接创建,但可以通过Pattern.compile (String regex) 简单工厂方法创建一个正则表达式

Pattern p = Pattern.compile ("\\w+");
方法功能
split (CharSequence input)将给定的输入序列分成这个模式的匹配
Matcher matcher (CharSequence input)提供了对正则表达式的分组支持,以及对正则表达式的多次匹配支持
Static boolean matches (String regex, CharSequence input)编译给定的正则表达式,并尝试匹配给定的输入
public class Demo12 {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("\\d+");
        String[] str = p.split("客户电话:16479534886,邮箱:suh@srea.com");
        System.out.println("是否匹配Pattern的输出模式:" + Pattern.matches("\\d+","2223"));
        System.out.println("是否匹配Pattern的输出模式:" + Pattern.matches("\\d+","2223aa"));
        Matcher m = p.matcher("22bb23");
        System.out.println("返回该Mattcher对象是由哪个Pattern对象创建的,即p为:" + m.pattern());
        System.out.print("将给定的字符串分割成Pattern模式匹配为:");
        for (int i = 0; i < str.length; i++) {
            System.out.println(str[i] + " ");
        }

    }
}

6.2.2 Matcher 类

Matcher 类用于在给定的Pattern实例的模式控制下进行字符串的匹配工作,同理,Matcher 类的构造方法也是私有的,不能直接创建只能通过Pattern.mattcher(CharSequence input) 方法得到该类的实例

方法功能
boolean matches ()对整个字符串进行匹配,只有整个字符串都匹配才返回true
boolean lookingAt ()对前面的字符串进行匹配,只有匹配到的字符串在最前面才返回true
boolean find ()对字符串进行匹配,匹配到的字符串可以在任何位置
int end ()返回最后一个字符匹配后的偏移量
string group ()返回匹配到的子字符串
int start ()返回匹配到的子字符串在字符串的索引位置
public class Demo13 {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher("22bb23");
        System.out.println("字符串是否匹配:" + m.matches());
        Matcher m2 = p.matcher("2223");
        System.out.println("字符串是否匹配:" + m2.matches());
        System.out.println("对前面的字符串匹配结果为:" + m.lookingAt());
        Matcher m3 = p.matcher("aa2223");
        System.out.println("对前面的字符串匹配结果为:" + m3.lookingAt());
        m.find();
        System.out.println("字符串任何位置是否匹配:" + m.find());
        m3.find();
        System.out.println("字符串任何位置是否匹配:" + m3.find());
        Matcher m4 = p.matcher("aabb");
        System.out.println("字符串任何位置是否匹配:" + m4.find());
        Matcher m1 = p.matcher("aaa2223bb");
        m1.find();
        System.out.println("上一匹配的起始索引:" + m1.start());
        System.out.println("最后一个字符匹配后的偏移量:" + m1.end());
        System.out.println("匹配到的子字符串:" + m1.group());
    }
}

6.3 String 类对正则表达式的支持

方法功能
boolean matches (String regex)匹配字符串
String replaceAll (String regex, String replacement)字符串替换
String [] split (String regex)字符串拆分
public class Demo14 {
    public static void main(String[] args) {
        String str = "A1B22DDS34DSJ9D";
        System.out.println("字符替换后为:" + str);
        boolean te = "321123as1".matches("\\d+");
        System.out.println("字符串是否匹配:" + te);
        String s [] = "SDS45d4DD4dDS88D".split("\\d+");
        System.out.println("字符串拆分后为:");
        for(int i = 0; i < s.length; i++){
            System.out.println(s[i] + " ");
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值