Arrays,Calendar,System,Date,Math和Random等类与正则表达式的基本用法

Arrays:
Arrays:针对数组操作的工具类

    常用的方法:

          public static String toString(int []arr):将int类型的数组转换为字符串(int[]arr-->"int[]arr")

           public static void sort(int[]arr):将数字数组按数字大小进行排序

         public static int binarySearch(int []arr,int value):二分搜索法:在int类型数组中寻找value元素的索引

package org.westos.Arrays;

import java.util.Arrays;

public class ArraysDemo1 {
public static void main(String[] args) {
	//定义一个数组
	int []arr= {13,45,12,34,86,99,56};
	String str=Arrays.toString(arr);//将int类型数组转换为字符串类型
	System.out.println(str);
	System.out.println("---------");
	Arrays.sort(arr);//将该数组进行排序
	System.out.println(Arrays.binarySearch(arr,2));//使用二分法查找索引
	System.out.println(Arrays.binarySearch(arr, 34));
	System.out.println(Arrays.binarySearch(arr, 666));
}
}

Calendar:

1.Calendar类:日历类

Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEARMONTHDAY_OF_MONTHHOUR日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。

Calendar类实例化:

     public static Calendar getInstance():通过静态方法来创建实例对象       

常用方法:

    public int get(int field):返回给定日期的值

   public static final int YEAR获取年

 public static final int MONTH获取月份

public static final int DATE获取该月份对应的日期

package org.westos.Clendar;

import java.util.Calendar;

public class ClendarDemo1 {
public static void main(String[] args) {
	//创建Calendar的实例对象
	Calendar cal=Calendar.getInstance();
	System.out.println(cal);
	//获取当前的日期
	int year=cal.get(Calendar.YEAR);
	int month=cal.get(Calendar.MONTH)+1;
	int date=cal.get(cal.DATE);
	System.out.println(year+"年"+month+"月"+date+"日");
}
}

public abstract void add(int field,int amount)根据日历的规则,为给定的日历字段添加或减去指定的时间量

public final void set(int year,int month,int date)设立日历字段YEAR,MONTH,

DAY_MONTH的值 

package org.westos.Clendar;

import java.util.Calendar;

public class CalendarDemo2 {
public static void main(String[] args) {
	Calendar cal=Calendar.getInstance();
	//给定日期
	cal.set(1996, 1, 35);
	//五年前的五天前
	cal.add(cal.YEAR,-5);
	cal.add(cal.DATE, -5);
    int year=cal.get(cal.YEAR);
    int month=cal.get(cal.MONTH)+1;
    int date=cal.get(cal.DATE);
    System.out.println(year+"年"+month+"月"+date+"日");

}
}
	
     
	
	
2. 需求:获取任意一年的二月有多少天  (改进:键盘录入一个年份)
package org.westos.Clendar;

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

public class CalendarDemo3 {
public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入一个年份");
	int year=sc.nextInt();
	Calendar cls=Calendar.getInstance();
	cls.set(year, 2, 1);
	cls.add(cls.DATE, -1);
	int month=cls.get(cls.MONTH)+1;
	int date=cls.get(cls.DATE);
	System.out.println(year+"年二月有"+date+"天");
}
}

System类:

1.System类包含一些用的类字段和方法,它不能被实例化

       常用的方法:
         public static void gc()运行垃圾回收器,实质是执行finalize方法

package org.westos.System;

public class Teacher {
   private String name;
   private int age;
   
public Teacher() {
	super();
	// TODO Auto-generated constructor stub
}

public Teacher(String name, int age) {
	super();
	this.name = name;
	this.age = age;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public int getAge() {
	return age;
}

public void setAge(int age) {
	this.age = age;
}

@Override
public String toString() {
	return "Teacher [name=" + name + ", age=" + age + "]";
}
//重写finalize()方法
@Override
protected void finalize()throws Throwable {
	System.out.println("垃圾回收器要回收此对象"+this);
	super.finalize();
}
   
}
package org.westos.System;

public class SystemDemo1 {
   public static void main(String[] args) {
	Teacher t=new Teacher("河正宇",36);
	System.out.println(t);
	t=null;
	System.gc();//垃圾回收器实际执行的是finalize()方法
}
}


        public static void exit(int status)终止当前运行的ava虚拟机.参数作为状态码:一般情况要终止jvm,参数为0

        public static long currentTimeMillis()返回当前时间的毫秒值

       public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
        从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束
src:原数组
dest:目标数组
srcPos :从原数组的哪个位置开始复制
destPos:到目标数组的哪个位置开始替换
length:要复制的数组长度
package org.westos.System;

import java.util.Arrays;

public class SystemDemo2 {
public static void main(String[] args) {
	long start=System.currentTimeMillis();
	int []arr= {12,3,45,6,98,97,61};
	int []arr1= {13,45,67,92,84,75,67};
	System.out.println(Arrays.toString(arr));
	System.out.println(Arrays.toString(arr1));
	System.out.println("----------");
	System.arraycopy(arr, 1,  arr1,1,6);
	System.out.println(Arrays.toString(arr));
	System.out.println(Arrays.toString(arr1));
	long end=System.currentTimeMillis();
	System.out.println("执行上面代码共耗时"+(end-start)+"毫秒");
	System.out.println("用了exit()方法,下面代码不会被执行了,不信看");
	System.exit(0);
	System.out.println("这句话能打印出来就活见鬼了");
}
}

Date:

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

     构造方法:

           public Date()表示当前时间,精确到毫秒

           public  void setTime(long time)设置时间毫秒值

           public Date(long date):创建一个日期对象,指定毫秒值 (需要将long 时间毫秒值转换成Date对象)

           public long getTime()将date转换为时间毫秒值

package org.westos.Date;

import java.util.Date;

public class DateDemo1 {
public static void main(String[] args) {
	//创建Date对象获取当前时间
	Date d1=new Date();
	System.out.println(d1);
	//d1获取给定时间
	d1.setTime(1000);
	System.out.println(d1);
	//创建Date对象获取给定时间
	Date d2=new Date(123456789);
	System.out.println(d2);
	//将d2的时间转换为时间毫秒值
	long time=d2.getTime();
	System.out.println(time);
}
}

2.Date的日期格式和日期的文本格式(String类型)之间的转换

  String-->Date(解析)

  Date-->String(格式化)

中间的转换:使用中一个中间类:DateFormat,并且DateFormat是一个抽象类,抽象意味着不能实例化,所以应该考虑用它的子类:
   SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
  
   SimpelDateFormat的构造方法:
   public SimpleDateFormat(String pattern) :构造一个SimpleDateFormat对象,根据pattern(模式:规则)
  
   SimpleDateFormat sdf = new SimpleDateFormat("xxx年xx月xx日") ;
 
  日期和时间模式
   
    年: yyyy
    月: MM
  日: dd
   
    时: hh
    分: mm
    秒: ss

   SimpleDateFormat常用的方法:

                public final String format(Date d)格式化,将Date的日期格式转换为日期的文本格式

               public Date parse(String str)解析,将日期的文本格式转换为Date的日期格式 

  注意 :simpleDateFormat在解析文本格式的时候,里面 模式(规则)一定要和文本格式的模式一直,否则就出现PareseException

package org.westos.Date;


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

public class DateDemo2 {
public static void main(String[] args) throws ParseException {
	//创建Date对象
	Date d1=new Date();
	System.out.println(d1);
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy--MM--dd--hh:mm:ss");
	//格式化,将Date的日期格式转换为日期的文本格式
	String str1=sdf.format(d1);
	System.out.println(str1);
	System.out.println("----------");
	String str3="1992--12--23--02:34:34";
	//解析,将日期的文本格式转换为Date的日期格式
	Date d3=sdf.parse(str3);
	System.out.println(d3);
	System.out.println("-------");
	String str2="1996年10月19日";
	SimpleDateFormat sdf2=new SimpleDateFormat("yyyy年MM月dd日");
	//解析,将日期的文本格式转换为Date的日期格式
	Date d2=sdf2.parse(str2);
	System.out.println(d2);
	System.out.println("-------");
	//格式化,将Date的日期格式转换为日期的文本格式
    System.out.println(sdf2.format(d1));
}
}

3.键盘录入你的出生年月日,算一下你来到这个世界多少天? 
package org.westos.Clendar;

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

public class DateText1 {
public static void main(String[] args) throws ParseException {
	Scanner sc =new Scanner(System.in);
	System.out.println("请输入你的出生日期");
	String str=sc.nextLine();
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy--MM--dd");
	Date d=sdf.parse(str);
    long old=d.getTime();
    System.out.println(old);
    long now=System.currentTimeMillis();
    System.out.println(now);
    long day=(now-old)/1000/3600/24;
    System.out.println("我来到这个世界上有"+day+"天了");
}
}

Math:

1.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 int min(int a,int b)获取最小值

     public static double pow(double a,double b)获取a的b次幂

     public static double sqrt(double a)获取a的正平方根

     public static double random()获取double类型的随机数,该值范围在[0.0,1.0)

    public static int round(float a)四舍五入

  

package org.westos.Math;

public class MathDemo {
public static void main(String[] args) {
	System.out.println(Math.abs(-23));
	System.out.println(Math.ceil(23.34));
	System.out.println(Math.floor(23.88));
	System.out.println(Math.random());
	System.out.println(Math.round(3.14));
	System.out.println(Math.pow(2.5, 4.5));
	System.out.println(Math.sqrt(4.4));
	System.out.println(Math.max(23.34, 12));
	System.out.println(Math.max(Math.min(23.34, 12),2.3));//可以用方法嵌套

}
}

Random:

Random:是一个可以获取随机数的类
 
 public Random():无参构造方法
 public Random(long seed) :指定long类型的数据进行构造随机数类对象 
 public int nextInt():获取随机数,它的范围是在int类型范围之内

 public int nextInt(int n):获取随机数,它的范围是在[0,n)之间

package org.westos.Random;

import java.util.Random;

public class RandomDemo {
public static void main(String[] args) {
	int b=23;
	Random r=new Random();
	while(b>1) {
	int a=r.nextInt(34);
	System.out.println(a);
	b--;
	}	
}
}

正则表达式:

字符


x x字符
\\ 反斜线字符
\t 制表符 
\n 换行符
\r 回车符 


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

预定义字符类:
. 任何字符 如果本身就是. \. qq.com 写正则表达式(\\.) 
\d 数字:[0-9] 写正则表达式 :\\d
\w 单词字符:[a-zA-Z_0-9]:字母大小写,数字字符 \\w


边界匹配器:
^ 行的开头 
$ 行的结尾 
\b 单词边界 尾 (helloword?haha:world)


Greedy 数量词(重点)
X? X,一次或一次也没有 
X* X,零次或多次 
X+ X,一次或多次 
X{n} X字符恰好出现n次
X{n,} X字符至少出现n次
X{n,m} X字符至少出现n次,但不超过m次

public String[] split(String regex) :字符串的分割功能

public String replaceAll(String regex,String replacement)

  使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 

package org.westos.正则表达式;

public class RegexDemo3 {
public static void main(String[] args) {
	String str="wojava2334466567xuede57568645367haixing21nibuyao788";
	System.out.println(str);
	String regex="\\d{2,3}";
	String str2="SOCCER";
	String str3=str.replaceAll(regex, str2);
	System.out.println(str3);
}
}

匹配器对象有一个方法:matches() ; 直接对当前字符串数据进行校验,返回boolean

1.需求:我有如下一个字符串:"91,27,46,38,50"
  请写代码实现最终输出结果是:"27 38 46 50 91" 

package org.westos.正则表达式;

import java.util.Arrays;

public class RegexDemo1 {
	public static void main(String[] args) {
		String str1 = "12,23,34,98,87,56,18";
		System.out.println(str1);
		String[] arr = str1.split(",");
		int[] arr2 = new int[arr.length];
		for (int i = 0; i < arr.length; i++) {
			arr2[i] = Integer.parseInt(arr[i]);
		}
		Arrays.sort(arr2);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < arr2.length; i++)
			sb.append(arr2[i]).append(" ");
		String str2 = sb.toString().trim();
		System.out.println(str2);
	}
}
2.键盘录入邮箱,校验邮箱
  邮箱:
  qq邮箱
  126邮箱
  163邮箱
  新浪微博邮箱
  企业邮箱 
  919081924@qq.com
  zhangsan@163.com
  xibukaiyuan@westos.com.cn
package org.westos.正则表达式;

import java.util.Scanner;


public class RegexDemo2 {
public static void main(String[] args) {	
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入一个邮箱");
	String str=sc.nextLine();
	String regex="\\w+@[a-zA-Z_0-9]+(\\.\\w+){1,4}";
	System.out.println(str.matches(regex));
}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值