Java SE day10_常用对象3

正则表达式(理解)

规则在java.util.regex Patterr类中
(1)就是符合一定规则的字符串
(2)常见规则
A:字符
x 字符 x。举例:‘a’表示字符a
\ 反斜线字符。
\n 新行(换行)符 (’\u000A’)
\r 回车符 (’\u000D’)

B:字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括

C:预定义字符类
. 任何字符。我的就是.字符本身,怎么表示呢? .
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成

D:边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi

E:Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
(3)常见功能:(分别用的是谁呢?)
A:判断功能
String类的public boolean matches(String regex)
B:分割功能
String类的public String[] split(String regex)
C:替换功能
String类的public String replaceAll(String regex,String replacement)

Pattern和Matcher类的使用

D:获取功能:
Pattern和Matcher
Pattern p = Pattern.compile(“a*b”);
Matcher m = p.matcher(“aaaaab”);

public boolean find():查找存不存在(必须先找)
public String group():获取刚才查找过的数据(才能返回)

package cn.itcast_05;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * 获取功能:
 * 获取下面这个字符串中由三个字符组成的单词
 * da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
 */
public class RegexDemo2 {
	public static void main(String[] args) {
		// 定义字符串
		String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
		// 规则
		String regex = "\\b\\w{3}\\b";

		// 把规则编译成模式对象
		Pattern p = Pattern.compile(regex);
		// 通过模式对象得到匹配器对象
		Matcher m = p.matcher(s);
		// 调用匹配器对象的功能
		// 通过find方法就是查找有没有满足条件的子串
		// public boolean find()
		// boolean flag = m.find();
		// System.out.println(flag);
		// // 如何得到值呢?
		// // public String group()
		// String ss = m.group();
		// System.out.println(ss);
		//
		// // 再来一次
		// flag = m.find();
		// System.out.println(flag);
		// ss = m.group();
		// System.out.println(ss);

		while (m.find()) {
			System.out.println(m.group());
		}

		// 注意:一定要先find(),然后才能group()
		// IllegalStateException: No match found
		// String ss = m.group();
		// System.out.println(ss);
	}
}

Math:用于数学运算的类(掌握)

成员变量:

  • public static final double PI
  • public static final double E 自然底数e的double值

成员方法:

  • public static int abs(int a):绝对值
  • public static double ceil(double a):向上取整
  • public static double floor(double a):向下取整
  • public static int max(int a,int b):最大值 (min自学)
  • public static double pow(double a,double b):a的b次幂
  • public static double random():随机数 [0.0,1.0)
  • public static int round(float a) 四舍五入(参数为double的自学)
  • public static double sqrt(double a):正平方根

案例(获取任意范围内的随机数)

import java.util.Scanner;

/*
 * 需求:请设计一个方法,可以实现获取任意范围内的随机数。
 * 
 * 分析:
 * 		A:键盘录入两个数据。
 * 			int strat;
 * 			int end;
 * 		B:想办法获取在start到end之间的随机数
 * 			我写一个功能实现这个效果,得到一个随机数。(int)
 * 		C:输出这个随机数
 */
public class MathDemo {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入开始数:");
		int start = sc.nextInt();
		System.out.println("请输入结束数:");
		int end = sc.nextInt();

		for (int x = 0; x < 100; x++) {
			// 调用功能
			int num = getRandom(start, end);
			// 输出结果
			System.out.println(num);
		}
	}

	/*
	 * 写一个功能 两个明确: 返回值类型:int 参数列表:int start,int end
	 */
	public static int getRandom(int start, int end) {
		// 回想我们讲过的1-100之间的随机数
		// int number = (int) (Math.random() * 100) + 1;
		// int number = (int) (Math.random() * end) + start;
		// 发现有问题了,怎么办呢?
		int number = (int) (Math.random() * (end - start + 1)) + start;
		return number;
	}
}

Random:产生随机数的类(理解)

构造方法:

  • public Random():没有给种子,用的是默认种子,是当前时间的毫秒值

  • public Random(long seed):给出指定的种子

  • 给定种子后,每次得到的随机数是相同的。

成员方法

  • public int nextInt():返回的是int范围内的随机数
  • public int nextInt(int n):返回的是[0,n)范围的内随机数
package cn.itcast_01;

import java.util.Random;

/*
 * Random:产生随机数的类
 * 
 * 构造方法:
 * 		public Random():没有给种子,用的是默认种子,是当前时间的毫秒值
 *		public Random(long seed):给出指定的种子
 *
 *		给定种子后,每次得到的随机数是相同的。
 *
 * 成员方法:
 * 		public int nextInt():返回的是int范围内的随机数
 *		public int nextInt(int n):返回的是[0,n)范围的内随机数
 */
public class RandomDemo {
	public static void main(String[] args) {
		// 创建对象
		// Random r = new Random();
		Random r = new Random(1111);

		for (int x = 0; x < 10; x++) {
			// int num = r.nextInt();
			int num = r.nextInt(100) + 1;
			System.out.println(num);
		}
	}
}

System类(掌握)

包含一些有用的类字段和方法。它不能被实例化。

  • System类包含一些有用的类字段和方法。它不能被实例化。

方法:

  • public static void gc():运行垃圾回收器和Object类里面的finalize性质一样。
  • public static void exit(int status):终止当前正在运行的 Java 虚拟机。
  • 参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
  • public static long currentTimeMillis():返回以毫秒为单位的当前时间
  • public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
  • 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
package cn.itcast_03;

import java.util.Arrays;

/*
 * System类包含一些有用的类字段和方法。它不能被实例化。 
 * 
 * 方法:
 * 		public static void gc():运行垃圾回收器。 
 *		public static void exit(int status):终止当前正在运行的 Java 虚拟机。
 *	参数用作状态码;根据惯例,非 0 的状态码表示异常终止。 
 *	 public static long currentTimeMillis():返回以毫秒为单位的当前时间
 *	 public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
 *				从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
 */
public class SystemDemo {
	public static void main(String[] args) {
		// 定义数组
		int[] arr = { 11, 22, 33, 44, 55 };
		int[] arr2 = { 6, 7, 8, 9, 10 };

		// 请大家看这个代码的意思
		System.arraycopy(arr, 1, arr2, 2, 2);

		System.out.println(Arrays.toString(arr));
		System.out.println(Arrays.toString(arr2));
	}
}

BigInteger类(理解)

  • BigInteger:可以让超过Integer范围内的数据进行运算

构造方法:

  • BigInteger(String val)

方法

  • public BigInteger add(BigInteger val):加
  • public BigInteger subtract(BigInteger val):减
  • public BigInteger multiply(BigInteger val):乘
  • public BigInteger divide(BigInteger val):除
  • public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组

BigDecimal类(理解)

  • 由于在运算的时候,float类型和double很容易丢失精度,演示案例。所以,为了能精确的表示、计 算浮点数,Java提供了BigDecimal

  • BigDecimal类:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。

构造方法:

  • public BigDecimal(String val)
  • public BigDecimal add(BigDecimal augend)
  • public BigDecimal subtract(BigDecimal subtrahend)
  • public BigDecimal multiply(BigDecimal multiplicand)
  • public BigDecimal divide(BigDecimal divisor)
  • public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取
    例如:System.out.println(“divide:”+ bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));

Date类(掌握)

Date:表示特定的瞬间,精确到毫秒。

构造方法:

  • Date():根据当前的默认毫秒值创建日期对象
  • Date(long date):根据给定的毫秒值创建日期对象

方法:

  • public long getTime():获取时间,以毫秒为单位
  • public void setTime(long time):设置时间

从Date得到一个毫秒值

  • getTime()

把一个毫秒值转换为Date
构造方法

  • setTime(long time)
  • Date – String(格式化)
  • public final String format(Date date)
  • String – Date(解析)
  • public Date parse(String source)
  • DateForamt:可以进行日期和字符串的格式化和解析,但是由于是抽象类,所以使用具体子类SimpleDateFormat。
  • SimpleDateFormat的构造方法
  • SimpleDateFormat():默认模式
  • SimpleDateFormat(String pattern):给定的模式
  • 这个模式字符串该如何写呢?
  • 通过查看API,我们就找到了对应的模式
  • 年 y
  • 月 M
  • 日 d
  • 时 H
  • 分 m
  • 秒 s
package cn.itcast_03;

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

/*
 * Date	 --	 String(格式化)
 * 		public final String format(Date date)
 * 
 * String -- Date(解析)
 * 		public Date parse(String source)
 * 
 * DateForamt:可以进行日期和字符串的格式化和解析,但是由于是抽象类,所以使用具体子类SimpleDateFormat。
 * 
 * SimpleDateFormat的构造方法:
 * 		SimpleDateFormat():默认模式
 * 		SimpleDateFormat(String pattern):给定的模式
 * 			这个模式字符串该如何写呢?
 * 			通过查看API,我们就找到了对应的模式
 * 			年 y
 * 			月 M	
 * 			日 d
 * 			时 H
 * 			分 m
 * 			秒 s
 * 
 * 			2014年12月12日 12:12:12
 */
public class DateFormatDemo {
	public static void main(String[] args) throws ParseException {
		// Date -- String
		// 创建日期对象
		Date d = new Date();
		// 创建格式化对象
		// SimpleDateFormat sdf = new SimpleDateFormat();
		// 给定模式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		// public final String format(Date date)
		String s = sdf.format(d);
		System.out.println(s);
		
		
		//String -- Date
		String str = "2008-08-08 12:12:12";
		//在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dd = sdf2.parse(str);
		System.out.println(dd);
	}
}

Data工具类

package cn.itcast_04;

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

/**
 * 这是日期和字符串相互转换的工具类
 * 
 * @author 风清扬
 */
public class DateUtil {
	private DateUtil() {
	}

	/**
	 * 这个方法的作用就是把日期转成一个字符串
	 * 
	 * @param d
	 *            被转换的日期对象
	 * @param format
	 *            传递过来的要被转换的格式
	 * @return 格式化后的字符串
	 */
	public static String dateToString(Date d, String format) {
		// SimpleDateFormat sdf = new SimpleDateFormat(format);
		// return sdf.format(d);
		return new SimpleDateFormat(format).format(d);
	}

	/**
	 * 这个方法的作用就是把一个字符串解析成一个日期对象
	 * 
	 * @param s
	 *            被解析的字符串
	 * @param format
	 *            传递过来的要被转换的格式
	 * @return 解析后的日期对象
	 * @throws ParseException
	 */
	public static Date stringToDate(String s, String format)
			throws ParseException {
		return new SimpleDateFormat(format).parse(s);
	}
}

Calendar日历类(掌握)

  • Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
  • 方法
  • public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。
package cn.itcast_01;

import java.util.Calendar;

/*
 * Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
 * 
 * public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。
 */
public class CalendarDemo {
	public static void main(String[] args) {
		// 其日历字段已由当前日期和时间初始化:
		Calendar rightNow = Calendar.getInstance(); // 子类对象

		// 获取年
		int year = rightNow.get(Calendar.YEAR);
		// 获取月
		int month = rightNow.get(Calendar.MONTH);
		// 获取日
		int date = rightNow.get(Calendar.DATE);

		System.out.println(year + "年" + (month + 1) + "月" + date + "日");
	}
}

/*
 * abstract class Person { public static Person getPerson() { return new
 * Student(); } }
 * 
 * class Student extends Person {
 * 
 * }
 */

方法:

  • public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。
  • public final void set(int year,int month,int date):设置当前日历的年月日

案例

package cn.itcast_03;

import java.util.Calendar;
import java.util.Scanner;

/*
 * 获取任意一年的二月有多少天
 * 
 * 分析:
 * 		A:键盘录入任意的年份
 * 		B:设置日历对象的年月日
 * 			年就是A输入的数据
 * 			月是2
 * 			日是1
 * 		C:把时间往前推一天,就是2月的最后一天
 * 		D:获取这一天输出即可
 */
public class CalendarTest {
	public static void main(String[] args) {
		// 键盘录入任意的年份
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份:");
		int year = sc.nextInt();

		// 设置日历对象的年月日
		Calendar c = Calendar.getInstance();
		c.set(year, 2, 1); // 其实是这一年的3月1日
		// 把时间往前推一天,就是2月的最后一天
		c.add(Calendar.DATE, -1);

		// 获取这一天输出即可
		System.out.println(c.get(Calendar.DATE));
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值