【Java入门】常用类-Math、Random、UUID、Date、Calendar、正则表达式


学习目标

一、Math

Math 类包含用于执行简单的数学运算的方法,如随机数、最大值最小值、指数等,该类的方法都是static修饰的,在开发中其实运用并不是很多,里面有一个求随机数的方法,偶尔会用到。

Math类位于java.lang包中。

Math常用方法:
在这里插入图片描述

public static void main(String[] args) {
		
		// [1]ceil/floor
		float a = 9.2f;
		// 比9.2大的最小整数:向上取整
		System.out.println(Math.ceil(a));
		// 比9.2小的最大整数:向下取整		
		System.out.println(Math.floor(a));
		
		// [2]求最值
		System.out.println(Math.max(10, 9));
		System.out.println(Math.min(10, 9));
		
		// [3]随机数 取值返回[0,1)
		System.out.println(Math.random());
		// 返回[m,n]区间的随机整数
		// (int)(Math.random()*(n-m+1)) + m;
}

二、Random

Random类用于生产一个伪随机数(通过相同的种子,产生的随机数是相同的),Math类的random方法底层使用的就是Random类的方式。

Random位于java.util包中。

常用方法
在这里插入图片描述

public static void main(String[] args) {

    // 1> Random
    Random r = new Random();
    // val的范围[0,100)之间
    int val = r.nextInt(100);
    System.out.println(val);
}

需求:随机产生4位的小写英文字母的验证码

/**
  *   分析:
  *   [1]. 产生一个a-z的随机字符c => 'a' + [0,25]
  *   [2]. 把产生的随机字符存入数组? 还是可变字符串?
  */
public static void main(String[] args) {
    StringBuilder codes = new StringBuilder(4);
    char c;
    Random rand = new Random();
    for(int i = 0;i < codes.capacity();i++){
        // [0,25]
        c = (char)('a' + rand.nextInt(26));
        codes.append(c);
    }
    System.out.println("codes = " + codes);
}

三、UUID

UUID表示通用唯一标识符 (Universally Unique Identifier) ,其算法通过电脑的网卡、当地时间、随机数等组合而成,优点是真实的唯一性,缺点是字符串太长了。

UUID位于java.util包中,jdk1.5 才出现的类

常用方法

在这里插入图片描述

public static void main(String[] args) {
    // 获取UUID对象
    UUID uuid = UUID.randomUUID();
    String str = uuid.toString();
    System.out.println("str = " + str);

    // 获取UUID中的前5个字母作为验证码 
    String code = uuid.substring(0, 5); 
    System.out.println(code); 
}

四、Date

Date 位于java.util包中,表示特定的瞬间,内部包装了一个long类型的fastTime,通过运算可以把fastTime转换成年月日时分秒。

常用方法

在这里插入图片描述

public class Test01 {
	public static void main(String[] args) {
		
		// 根据系统当前时区,当前日期时间构建一个Date对象
        Date now = new Date();
        // Wed Aug 11 10:52:20 CST(chinese standard time) 2021
        System.out.println(now.toString());
		
		// 获取date对象的时间戳
		long ts = now.getTime();
		System.out.println(ts);
	}
}

4.1、SimpleDateFormat

打印Date对象时,默认打印的是欧美人的日期时间风格,如果需要输出自定义的时间格式,比如2020年12月12日 12:12:12格式或者2020-12-12 12:12:12,此时可以使用SimpleDateFormat类。

SimpleDateFormat类,顾名思义是日期的格式化类,主要包括两个功能的方法:

  • 格式化(format):Date类型转换为String类型:String format(Date date)
  • 解析(parse):String类型转换为Date类型:Date parse(String source)

无论是格式化还是解析都需要设置日期时间的模式,所谓模式就是一种格式。
在这里插入图片描述
日期模式举例:

yyyy-MM-dd							如2020-12-12
HH:mm:ss							如201212
yyyy-MM-dd HH:mm:ss					如2020-12-12 201212
yyyy/MM/dd HH:mm:ss					如2020/12/12 201212
yyyy年MM月dd日 HH时mm分ss秒			如20201212201212

常用方法
在这里插入图片描述

public class SimpleDateFormatDemo {
	public static void main(String[] args) throws ParseException {
        
		Date date = new Date();
		// 创建SimpleDateFormat对象
		SimpleDateFormat df = new SimpleDateFormat();
		String pattern = "yyyy-MM-dd HH:mm:ss";
        // 设置日期时间转换模式
		df.applyPattern(pattern);
        
		// 格式化(format):Date类型转换为String类型:String format(Date date)
		String dateStr = df.format(date);
		System.out.println(dateStr);
        
        
		// 解析(parse):String类型转换为Date类型:Date parse(String source)
		Date date2 = df.parse(dateStr);
		System.out.println(date2.toString());
	}
}

五、Calendar

Calendar 日历类,其内部封装了一个long time 表示时间戳,其内部提供了方法通过对time计算出年月日时分秒…等日历字段,这些字段都被存储到一个数组中,通过get(字段)可以去数组中提取对于字段的值。

Calendar本身是一个抽象类,通过getInstance方法获取对象,其底层创建的是Calendar的子类对象。

Calendar还提供了用来对日期做相加减,重新设置日期时间功能

public class CalendarDemo1 {
	public static void main(String[] args) throws Exception {
		// 根据当前地区,当前语言环境,当前时间构造一个通用的日历对象
		Calendar cal = Calendar.getInstance();
		System.out.println(cal);
		
		// 获取日历字段信息
		// 获取日历中的年月日
		System.out.println(cal.get(Calendar.YEAR));
		// 月从0开始,0表示1月,1表示2月...
		System.out.println(cal.get(Calendar.MONTH));
		System.out.println(cal.get(Calendar.DATE));
		// System.out.println(cal.get(Calendar.DAY_OF_MONTH));
		
		System.out.println(cal.get(Calendar.HOUR));
		System.out.println(cal.get(Calendar.MINUTE));
		System.out.println(cal.get(Calendar.SECOND));
		
		// 获取星期(周几)
		// 一周的第一天是周日
		System.out.println(cal.get(Calendar.DAY_OF_WEEK));
		
        // 增加日历字段对于的值
		cal.add(Calendar.YEAR, 100);//在当前年份上增加100
		System.out.println(c.get(Calendar.YEAR));//2118
	}
}

需求1:查询某个时间最近一周的信息,如何表示最近这一周的开始时间和结束时间

假如给出时间为:2018-05-18 15:05:30,那么最近一周的开始和结束时间分别为:

开始时间:2018-05-12 00:00:00

结束时间:2018-05-18 23:59:59

public class CalendarDemo2 {
	public static void main(String[] args) throws Exception {
        // [1] 通过字符串解析出时间对象
		String input = "2018-05-18 15:05:30";// 输入时间
		String pattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(pattern);
		Date d = sdf.parse(input);
        
        // [2] 调整日历时间
		Calendar c = Calendar.getInstance();
		c.setTime(d);// 把当前输入时间转换为Calendar对象
        
        // [3] 计算结束时间
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		Date endDate = c.getTime();
		System.out.println(endDate.toLocaleString());
        
        // [4] 计算开始时间
		c.add(Calendar.SECOND, 1);// 秒数增加1
		c.add(Calendar.DAY_OF_MONTH, -7);// 天数减去7
		Date beginDate = c.getTime();
		System.out.println(beginDate.toLocaleString());
	}
}

六、正则表达式

正则表达式,简写为regex和RE/Re。

正则表达式用来判断某一个字符串是不是符合某一种规则,在开发中通常用于判断检测操作、替换操作、分割操作等。

在这里插入图片描述
正则表达式规则

  • 正则表达式匹配规则一:元字符 ( 正则表达式中已定义好的字符,这些字符有特定的含义 )
    在这里插入图片描述
  • 正则表达式匹配规则二:量词
    在这里插入图片描述

正则表达式的练习

判断一个字符串是否全部有数字组成

判断一个字符串是否是手机号码

判断一个字符串是否是18位身份证号码

判断一个字符串是否6到16位,且第一个字必须为字母


public class REDemo {
	public static void main(String[] args) throws Exception {
		// 判断一个字符串是否全部有数字组成
		System.out.println("12345678S".matches("\\d"));// false
		System.out.println("12345678".matches("\\d"));// false
		System.out.println("12345678".matches("\\d*"));// true
		System.out.println("1234".matches("\\d{5,10}"));// false
		System.out.println("12345678".matches("\\d{5,10}"));// true
        
		// 判断一个字符串是否是手机号码
		String regex1 = "^1[3|4|5|7|8][0-9]{9}$";
		System.out.println("12712345678".matches(regex1));// false
		System.out.println("13712345678".matches(regex1));// true
        
		// 判断一个字符串是否是18位身份证号码
		String regex2 = "\\d{17}[[0-9]X]";
		System.out.println("511123200110101234".matches(regex2));// true
		System.out.println("51112320011010123X".matches(regex2));// true
		System.out.println("51112320011010123S".matches(regex2));// false
		
        // 判断一个字符串是否6到16位,且第一个字必须为字母
		String regex3 = "^[a-zA-Z]\\w{5,15}$";
		System.out.println("will".matches(regex3));// false
		System.out.println("17will".matches(regex3));// false
		System.out.println("will17willwillwill".matches(regex3));// false
		System.out.println("will17".matches(regex3));// true
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值