黑马程序员——Java常用类笔记(下)【Math类、Random类、System类、Date类】

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


常用类总结(下)


目录:


五、正则表达式(Pattern,Matcher)
六、Math类
Random类
System类
七、BigInteger类/BigDecimal类
八、Date类/DateFormat类/Calendar类
-------------------------------------------------
--------------------------------------------------


五, 正则表达式(Pattern,Matcher)

1)概述
是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。
其实就是一种规则。
有自己特殊的应用。

示例:简单认识正则

public class RegexDemo_qq {
	/*qq:708550943  要求5到15位  开头不能为0
	  需求:验证是否是qq号
	*/
	public static void main(String[] args) {
		String qq="08920943";
		System.out.println(method(qq));
	}
	public static boolean method(String qq){
		String regex="[^0]\\d{5,15}";
		//利用String类的matches方法
		return qq.matches(regex);
	}
}

2) 正则表达式的应用

判断功能 public boolean matches(String regex)
示例:

/*
检验邮箱:
*/
public static boolean method(String st){
		//正则表达式[]内的字符为或的关系. 所以com没有必要加[]. 
		//String regex="[a-zA-Z_\\d]+@[a-zA-Z\\d]+\\.com";
		//满足.com.cn.us的需求 
		String regex="[a-zA-Z_\\d]+@[a-zA-Z\\d]+(\\.[a-z]+)+";
		return st.matches(regex);		
	}

分割功能public String[] split(String regex)
示例:

/*
我有如下一个字符串:”91 27 46 38 50”
请写代码实现最终输出结果是:”27 38 46 50 91”
*/
	public static void method1(String s){
		//定义用于分割的正则表达式
		String regex=" +";
		//调用字符串的split()方法
		String[] strArr = s.split(regex);
		for(int i=0;i<strArr.length-1;i++){
			for(int j=i+1;j<strArr.length;j++){
				//数字字符串比较大小
				if(strArr[i].compareTo(strArr[j])>0){
					String t=strArr[i];
					strArr[i]=strArr[j];
					strArr[j]=t;
				}
			}
		}
		//强for循环
		for(String ss:strArr){
			System.out.print(ss+" ");
		}
		
	}
}

替换功能public String replaceAll("正则规则","替换后的新内容")
示例:

/**
	 * 实现替换功能,将符合正则表达式的的字符串替换为指定字符串.
	 * replaceAll();
	 * s="708920943@qq.com"
	 * 将qq替换为163
	 * */
	public static String method2(String s){
		String ragex="[q]+";
		String s=s.replaceAll(ragex, "163");		
		return s;
	}

获取功能:需要Pattern类\ Matcher类 的配合使用

示例:

/*
 *  获取由二个字符组成的单词
 * 在字符串s="wo ai ni ma? nan dao ni bu zhi dao ma?"
 */
	public static void method5(){

		//通过compile方法,将正则规则预编译到Pattern对象里
		Pattern p = Pattern.compile("\\b[a-z]{2}\\b");

		//将预编译规则  和要查找的字符串匹配起来, 返回到Matcher的对象里
		Matcher m = p.matcher("wo ai ni ma? nan dao ni bu zhi dao ma?");

		//m.find();判断下一个是不是匹配字符串. 返回值为波尔型
		while(m.find()) {
			//m.group() 返回找到的字符串
			System.out.println(m.group());
		}
	}

六、Math类\Random类\System类


 1,Math类概述
Math 类包含用于执行基本数学运算的方法,
如初等指数、对数、平方根和三角函数。 


成员方法
//求绝对值
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) 
//返回第一个参数的第二个参数次幂的值。
public static double pow(double a,double b)
//返回随机的 double 值,该值大于等于 0.0 且小于 1.0。
public static double random()
//返回最接近参数的 int。
public static int round(float a) 
//返回正确舍入的 double 值的正平方根。
public static double sqrt(double a)

 2,Random类概述
此类用于产生随机数
如果用相同的种子创建两个 Random 实例,
则对每个实例进行相同的方法调用序列,
它们将生成并返回相同的数字序列。

构造方法
//创建一个新的随机数生成器。
public Random()
// 使用单个 long 种子创建一个新的随机数生成器。
public Random(long seed)


成员方法:
//返回下一个伪随机数,它是此随机数生成器的序列中
//均匀分布的 int 值。
public int nextInt()
//返回下一个伪随机数,它是此随机数生成器的序列中
//在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
public int nextInt(int n)


示例:猜字游戏

import java.util.Scanner;
class Gust{
	public static int getNum(){
	
		return (int)( Math.random()*100+1);// 随机生成1-100的整数'
	}
}
class GuestDemo{
	public static void main(String[]args){

		//创建具有键盘输入功能的对象s
		Scanner s=new Scanner(System.in);
		//获取随机数
		int num=Gust.getNum();
		while (true){
			//nextInt()  具有接收整型输入的方法
			int n=s.nextInt();
			if(n>num){
				System.out.println("猜大了");	
			}else if(n<num){

				System.out.println("猜小了");	
			}
			else{
				System.out.println("猜中了");	
				break;
			}
		}
	}
}

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

成员方法
//终止当前正在运行的 Java 虚拟机。
public static void exit(int status)
// 返回以毫秒为单位的当前时间。
public static long currentTimeMillis()
//从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)


七、BigInteger类/BigDecimal类


1,BigInteger类概述

BigInteger将超级大数,封装成,BigInteger对象来进行运算.
可以让超过Integer范围内的数据进行运算
该类实现大数据运算时,要求是整数.
一个超过long型取值范围的数,不能算作整数.

构造方法

//将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
public BigInteger(String val)


//将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。
public BigInteger(String val, int radix) 
    
   成员方法
//相加
public BigInteger add(BigInteger val)
//相减
public BigInteger subtract(BigInteger val)
//相乘
public BigInteger multiply(BigInteger val)
//除法
public BigInteger divide(BigInteger val)


示例: 大数据 BigInteger 的四则运

public class BigIntegerDemo {
	public static void main(String[] args) {
		method_1();
	}
	/*
	 * 计算大数据的四则运算
	 * add,subtract multiply divide
	 * 参于运算的都是BigInteger类的对象,运算结果都是BigInteger对象
	 */
	public static void method_1(){
		BigInteger b1 = new BigInteger("2134564324562347654324567876542345632454");
		BigInteger b2 = new BigInteger("134564324567654324567875632454");
		
		//+ 运算 方法add
		BigInteger bigadd = b1.add(b2);
		System.out.println("b1+b2::"+bigadd);
		
		//- 运算 方法subtract
		BigInteger bigsub = b1.subtract(b2);
		System.out.println("b1-b2::"+bigsub);
		
		//* 运算 方法 multiply
		BigInteger bigmul = b1.multiply(b2);
		System.out.println("b1*b2::"+bigmul);
		
		// / 运算 方法 divide
		BigInteger bigdiv = b1.divide(b2);
		System.out.println("b1/b2::"+bigdiv);
	}

2,BigDecimal类概述

  由于在运算的时候,float类型和double很容易丢失精度,演示案例.
所以,为了能精确的表示,计算浮点数,Java提供了BigDecimal
不可变的、任意精度的有符号十进制数。

构造方法
//将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)
 
示例:实现大数据的除法运算

/*
 * 实现大数据浮点的除法运算
 * divide(BigDecimal divisor, int scale, int roundingMode) 
 * 第一个参数,被除数是一个BigInteger对象
 * 第二个参数,保留位数
 * 第三个参数,舍入模式
 * 舍入模式  BigDeimal.ROUND_UP  向上加1
 * 舍入模式  BigDeimal.ROUND_DOWN  直接舍去
 * 舍入模式  BigDeimal.ROUND_HALF_UP  四舍五入
 */
	public static void method_3(){
		BigDecimal b1 = new BigDecimal("123.54");  
		BigDecimal b2 = new BigDecimal("10");
		
		BigDecimal b3 = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);
		System.out.println(b3);
	}

示例:小数运算不确定因素

/*
 * 计算程序中出现的小数运算导致的不确定因素
 */
	public static void method(){
		System.out.println(0.09 + 0.01);
		System.out.println(1.0 - 0.32);
		System.out.println(1.015 * 100);
		System.out.println(1.301 / 100);

	}

八、Date类/DateFormat类/Calendar类

1,Date类概述
类 Date 表示特定的瞬间,精确到毫秒。 

构造方法
//分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。
public Date()

//分配 Date 对象并初始化此对象,以表示自从标准基准时间
//以来的指定毫秒数。
public Date(long date)
成员方法
//返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
public long getTime()
//设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
public void setTime(long time)
示例:

/*
 * Date类中,构造方法,setTime()与getTime()方法的应用.
 * */
	public static void method_1(){
		Date d =new Date();
		long time=d.getTime();//得到这个日期的毫秒值
		System.out.println(time);
		System.out.println(d);//与毫秒值time匹配的系统日期(系统格式).
		
		//重写配置时间
		d.setTime(1334852026929L);//为日期重置毫秒值,来改变日期.
		System.out.println(d.getTime());
		System.out.println(d);
		//返回以毫秒为单位的当前时间。long类型
		long l=System.currentTimeMillis();
		System.out.println(l);
	}

2,DateFormat类
DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。
是抽象类,所以使用其子类SimpleDateFormat.

使用 getDateInstance() 来获取该国家/地区的标准日期格式。
使用 getTimeInstance() 可获取该国家/地区的时间格式。
使用 getDateTimeInstance() 可获取日期和时间格式。
可以将不同选项传入这些工厂方法,以控制结果的长度(从 SHORT 到 MEDIUM 到 LONG 再到 FULL)。
确切的结果取决于语言环境,但是通常: 
SHORT 完全为数字,如 12.13.52 或 3:30pm 
MEDIUM 较长,如 Jan 12, 1952 
LONG 更长,如 January 12, 1952 或 3:30:32pm 
FULL 是完全指定,如 Tuesday、April 12、1952 AD 或 3:30:42pm PST
构造方法
//创建一个新的 DateFormat。
public SimpleDateFormat()
//用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
public SimpleDateFormat(String pattern)

成员方法
//将一个 Date 格式化为日期/时间字符串。
public final String format(Date date)
//从给定字符串的开始解析文本,以生成一个日期。
public Date parse(String source)
//返回DateFormat类的子类对象,参数中可以写SHORT\MEDIUM\LONG\FULL选型.获取长度不同的表示形式.
public static final DateFormat getDateTimeInstance();

示例:getDateTimeInstance()的应用.

public static void method_3() {
	//创键了日期风格对象,并规定了 日期\时间表示形式的长度.
	DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.LONG);
	//将日期风格化为已规定了样式的字符串.
	String s = df.format(new Date());
	System.out.println(s);

示例:public Date parse(String source)的应用:

public static void method_2()throws Exception{
		DateFormat df=DateFormat.getDateInstance(DateFormat.SHORT);
		//有可能输入错误,因此需要抛异常
		//将字符串转为日期对象
		Date d=df.parse("2015-10-3");
		//将日期对象转为字符串
		String s = df.format(d);
		System.out.println(s);
}

示例:计算计算来到这个世界多少天

import java.util.*;
import java.text.*;
public class DateTest {
	public static void main(String[] args) throws Exception{
		//键盘输入
		String birthdayString = new Scanner(System.in).nextLine();
		//获取DateFormat对象
		DateFormat df = DateFormat.getDateInstance();
		//parse,字符串转成日期对象
		Date birthdayDate = df.parse(birthdayString);
		//今天的日期,和出生日期转成毫秒值
		long birthdayTime = birthdayDate.getTime();
		long todayTime = new Date().getTime();
		
		if(birthdayTime > todayTime){
			System.out.println("没出生");
			return ;
		}
		
		//两个日期相差的毫秒值
		long time = Math.abs(birthdayTime - todayTime);
		
		System.out.println(time/1000/60/60/24);
	}
}

 PS   :SimpleDateFormat类继承DateFormat类,SimpleDateFormat 是非抽象类.(常用)
构造方法:
//用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat.
SimpleDateFormat(); 
//用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat
SimpleDateFormat(String pattern); 
示例: 

public  static void method_4(){
		//a 是来表示上午\下午.
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 ahh点mm分ss秒");
		//将日期对象转为字符串(已经被风格化的)
		String s = sdf.format(new Date());
		System.out.println(s);
	}

3,Calendar类概述

Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 
日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

成员方法
//创造一个Calendar对象
public static Calendar getInstance()
//返回给定日历字段的值.
public int get(int field)
//根据日历的规则,为给定的日历字段添加或减去指定的时间量。
public void add(int field,int amount)
//将给定的日历字段设置为给定值
public final void set(int year,int month,int date)
示例:
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.MONTH);
System.out.println(year);


示例:打印生产日期,保质期限

public static void method() throws Exception{
		
		//设定生产日期
		 String s= "2012年1月1日";
		 
		 //保质期限位12个月
		int b=12;
		
		//创建风格化对象.
		SimpleDateFormat spff=new SimpleDateFormat("yyyy年MM月dd日");
		
		//将输入的字符串 转化为日期对象.
		Date d = spff.parse(s);
		
		//创建日历对象.
		Calendar cal=Calendar.getInstance();
		
		//为日历对象设置日期.
		cal.setTime(d);
		
		//cal.getTime()从日历中取出日期,format()将日期转化为字符串
		String start=spff.format(cal.getTime());
		
		//打印设置的日期(生产日期)
		System.out.println("生产日期为      "+start);
		
		//对日历偏移
		cal.add(cal.MONTH, b);
		
		//为偏移后的日期风格化
		String end=spff.format(cal.getTime());
		
		//偏移后的日期(保质期已到)
		System.out.println("到期日期为: "+end);
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值