正则表达式的概述和简单使用

14.01_常见对象(正则表达式的概述和简单使用)

  • A:正则表达式
    • 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。(对于一些用户名或密码的校验)
  • B:案例演示

    • 需求:校验qq号码.

      • 1:要求必须是5-15位数字
      • 2:0不能开头
      • 3:必须都是数字
    • a:非正则表达式实现

    • b:正则表达式实现
    • String regex = "[1-9][\\d]{4,14}";
      		//正则表达式,[1-9]输入是1-9之间的,[\\d]{4,14}1~9的数字出现次数4~14次之间,加上[1-9]这个一次就是5~15次
      		System.out.println("abcdef".matches(regex));
      		//String的matches(regex)方法:是看字符串是否与regex正则表达式匹配
      		System.out.println("2553868".matches(regex));
      		System.out.println("012345".matches(regex));
      		System.out.println("1234567890987654".matches(regex));
      ******************************
      false
      true
      false
      false

ps:Character.isDigit(temp):判断指定字符temp是不是数字。

String的matches(regex)方法:是看字符串是否与regex正则表达式匹配

14.02_常见对象(字符类演示)

  • A:字符类
    • [abc] a、b 或 c(简单类) 
    • 		String regex = "[123]";	//中括号中代表的是单个字符,与正则表达式匹配的是1或2或3单个字符
      		System.out.println("12".matches(regex));
      		System.out.println("d".matches(regex));
      		System.out.println("1".matches(regex));
      *********************************
      false
      false
      true
      [^abc] 任何字符,除了 a、b 或 c(否定) 
    • 		String regex = "[^abc]";//除了a,b,c的单个字符(^是取反)
      		System.out.println("a".matches(regex));
      		System.out.println("10".matches(regex));//[]强调的是单个字符,而"10"是两个字符'1','0'
      		System.out.println("d".matches(regex));
      ******************************
      false
      false
      true
      [a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围) 
    • 		String regex = "[a-z[A-Z]]";//[]强调单个字符
      		System.out.println("a".matches(regex));
      		System.out.println("Z".matches(regex));
      		System.out.println("M".matches(regex));
      		System.out.println("ab".matches(regex));
      		System.out.println("1".matches(regex));
      ********************************
      true
      true
      true
      false
      false
      [a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
    • String regex = "[a-d[m-p]]";
      		System.out.println("c".matches(regex));
      		System.out.println("e".matches(regex));
      ****************************
      true
      false
      [a-z&&[def]] d、e 或 f(交集) 
    • 		String regex = "[a-z&&[def]]";//取交集
      		System.out.println("d".matches(regex));
      		System.out.println("a".matches(regex));
      ********************************
      true
      false
      [a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去) 
    • 		String regex = "[a-z&&[^bc]]";
      		System.out.println("b".matches(regex));
      		System.out.println("c".matches(regex));
      		System.out.println("a".matches(regex));
      ******************************
      false
      false
      true
      [a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去) 

:[ ]表示的是单个字符

14.03_常见对象(预定义字符类演示)

  • A:预定义字符类
    • . 任何字符 
    • 		String regex = "..";	//一个点就代表任意的一个字符,n个点代表n歌字符
      		System.out.println("a".matches(regex));
      		System.out.println("aa".matches(regex));
      		System.out.println("%%".matches(regex));
      ***********************************
      false
      true
      true
      \d 数字:[0-9] 单个字符
    • 		String regex = "\\d";//在字符串中\是转义字符,
      		System.out.println("10".matches(regex));//"10"是两个字符
      *****************************
      false
      \D 非数字: [^0-9] 
      \s 空白字符:[ \t\n\x0B\f\r] (空格,制表符,换行,垂直制表符,翻页,回车)
    • 		String regex = "\\s";
      		System.out.println(" ".matches(regex));
      		System.out.println("    ".matches(regex));//四个空格不是单个字符了
      		System.out.println("	".matches(regex));//一个tab键是单个字符
      		System.out.println("a".matches(regex));
      *********************************
      true
      false
      true
      false
      \S 非空白字符:[^\s] (\s取反)
      \w 单词字符:[a-zA-Z_0-9] 
      • \W 非单词字符:[^\w] 
    • 		String regex = "\\W";//非单词字符
      		System.out.println(" ".matches(regex));
      		System.out.println("a".matches(regex));
      		System.out.println("9".matches(regex));
      **********************************
      true
      false
      false
    • 注: []表示单个字符
      * \表示转义符
  • 14.04_常见对象(数量词)

  • A:Greedy 数量词
    • X? X一次或一次也没有
    • 		String regex = "[abc]?";//单个字符a或b或c出现一次或一次也没有 
      		System.out.println("a".matches(regex));
      		System.out.println("d".matches(regex));
      		System.out.println("".matches(regex));//abc一个都没出现,且什么其他字符也没没有出现
      		System.out.println("ab".matches(regex));//regex对单个字符而言
      *******************************
      true
      false
      true
      false
    • X* X零次或多次(0到无穷)
    • X+ X一次或多次(1到无穷)
    • X{n} X恰好 n 次
    • X{n,} X至少 n 次
    • X{n,m} X至少 n 次,但是不超过 m 次

14.05_常见对象(正则表达式的分割功能)

  • A:正则表达式的分割功能:根据给定正则表达式的匹配拆分字符串,切割后返回的是字符串数组
    • String类的功能:public String[] split(String regex)
  • B:案例演示
    • 正则表达式的分割功能
    • 		String s = "我..爱....java";
      		String regex = "\\.+";	
      		//.是代表任意字符,不能直接用点切割(即regex = "."造成结果遇见每个字符都切),需要转义(即:regex = "\\.+"表示遇见"."一次或多次切)
      		String[] arr = s.split(regex);	//切割后返回的是字符串数组
      		for (int i = 0; i < arr.length; i++) {
      			System.out.println(arr[i]);
      		}
      ********************************
      我
      爱
      java

14.06_常见对象(把给定字符串中的数字排序)

  • A:案例演示
    • 需求:我有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”
    • * 1,将字符串切割成字符串数组(利用split())
      * 2,定义一个与String数组长度相同的int型数组,将String数组中的字符串转换后存储在该数组中
      * 3,对int数组排序(Arrays.sort()方法)
      * 4,将int数组转换成字符串(将int数组转化成StringBuilder,再调用toString方法)
    •  
      String s = "91 27 46 38 50";
      		String[] arr = s.split(" ");			//将字符串切割成字符串数组
      		int[] iArr = new int[arr.length];		//定义int类型的数组,长度与String数组相同
      		
      		for (int i = 0; i < arr.length; i++) {	//遍历字符串数组
      			iArr[i] = Integer.parseInt(arr[i]); //将字符串中的每一个数字字符串转成数字
      		}
      		
      		Arrays.sort(iArr);						//排序
      		
      		System.out.println(arrToString(iArr));	//将int数组转换成字符串
      		
      	}
      
      	public static String arrToString(int[] arr) {
      		StringBuilder sb = new StringBuilder();
      		//sb.append("[");
      		for (int i = 0; i < arr.length; i++) {
      			if(i == arr.length - 1) {
      				sb.append(arr[i]);
      			}else {
      				sb.append(arr[i]).append(", ");
      			}
      		}
      		
      		return sb.toString();//StringBuilder 转换成String
      	}
      ***************************
      27, 38, 46, 50, 91
    • 14.07_常见对象(正则表达式的替换功能)
  • A:正则表达式的替换功能
    • String类的功能:public String replaceAll(String regex,String replacement)
  • B:案例演示
    • 正则表达式的替换功能
    • 		demo1();
      		demo2();
      		demo3();
      		//出现四个一模一样的字符哈哈哈哈
      		String regex = "(.)\\1{3}";
      		System.out.println("1234".matches(regex));
      		System.out.println("哈哈哈哈".matches(regex));
      	}
      
      	public static void demo3() {
      		//判断是否是叠词高兴高兴,快乐快乐,死啦死啦
      		String regex = "(..)\\1";//两个任意字符组成的一个组又出现一次,即出现了两次
      		System.out.println("死啦死啦".matches(regex));
      		System.out.println("1234".matches(regex));
      	}
      
      	public static void demo2() {
      		//判断是否是叠词。高高兴兴,快快乐乐
      		String regex = "(.)\\1(.)\\2";//第一个词是任意的所有是".",(.)\\1表示这个组又出现一次
      		System.out.println("不高兴兴".matches(regex));
      		System.out.println("快快乐乐".matches(regex));
      	}
      
      	public static void demo1() {
      		String s = "itcast";
      		String regex = "[abc]";
      		String s2 = s.replaceAll(regex, "oo");//利用正则表达式找到s中需要替换的字符。将找到的字符替换成"oo",返回一个新的字符串
      		System.out.println(s2);
      	}
      
      }
      ******************************
      itoooost
      false
      true
      true
      false
      false
      true


    • 14.08_常见对象(正则表达式的分组功能)
  • A:正则表达式的分组功能
    • 捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 ((A)(B(C))) 中,存在四个这样的组:
  • 1     ((A)(B(C))) 
    2     (A 
    3     (B(C)) 
    4     (C) 
    
    组零始终代表整个表达式。
    

    B:案例演示 a:切割 需求:请按照叠词切割: "sdqqfgkkkhjppppkl";(.)\1+ b:替换 需求:我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程 将字符串还原成:“我要学编程”。

  • String s = "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程";
    		String s2 = s.replaceAll("\\.+", "");//把.换掉
    		String s3 = s2.replaceAll("(.)\\1+", "$1");		//$是获取对应组中的字符
    		System.out.println(s3);
    ****************************
    我要学编程


14.09_常见对象(Pattern和Matcher的概述)

  • A:Pattern和Matcher的概述
  • B:模式和匹配器的典型调用顺序

    • 通过JDK提供的API,查看Pattern类的说明

    • 典型的调用顺序是

    • Pattern p = Pattern.compile("a*b"); //获取正则对象
    • Matcher m = p.matcher("aaaaab"); //调用Pattern的matcher()方法返回获取匹配器
    • boolean b = m.matches(); //调用matches()方法,判断是不是符合正则标准
    • 		String str = "我的电话号码是18511866260,曾经用过13887654321,还用过18912345678";//提取电话号码
      		String regex = "1[34578]\\d{9}";//正则表达式第一位1,[34578]第二位可是3,4,5,7,8中的一个,\\d{9}:1~9中的任意字符9次
      		Pattern p = Pattern.compile(regex);//获取正则对象
      		Matcher m = p.matcher(str);//获取匹配器
      		//Matcher匹配器中的方法:find():在字符串str中是否找到符合regex的小字符串,能找到返回true,不能找到返回false
      		while(m.find())		//当while语句只控制一句语句的时候,大括号可以省略
      			System.out.println(m.group());//group():返回满足regex的字符串
      ****************************************
      18511866260
      13887654321
      18912345678
      

      :Matcher类中的方法:
    • find():在字符串中是否能找到符合正则表达式的字符串,能返回true,否则返回false
    • group():返回满足正则表达式的字符串

14.10_常见对象(正则表达式的获取功能)

  • A:正则表达式的获取功能
    • Pattern和Matcher的结合使用
  • B:案例演示
    • 需求:把一个字符串中的手机号码获取出来

14.11_常见对象(Math类概述和方法使用)

  • A:Math类概述(工具类,没有构造方法,且都是静态方法)
    • Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
  • B:成员方法
    •  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之间的数,不包括1.0
      * public static int round(float a) 参数为double的自学//四舍五入
      * public static double sqrt(double a)//a开平方
    • 		System.out.println(Math.PI);
      		System.out.println(Math.abs(-10));				//取绝对值
      		System.out.println(Math.abs(10));					
      		System.out.println(Math.ceil(12.33));		//天花板(向上取整,但是是一个double数)
      		System.out.println(Math.ceil(12.77));
      		System.out.println(Math.floor(12.33));	//地板(向下取整,但是是一个double数)
      		System.out.println(Math.floor(12.77));
      		System.out.println(Math.max(10, 100));
      		System.out.println(Math.pow(2, 3));	 //2.0 ^ 3.0次方
      		System.out.println(Math.random());	//随机数(0.0 - 1.0之间,不包括1.0)
      		System.out.println(Math.round(12.33));
      		System.out.println(Math.round(12.53));			//四舍五入
      		System.out.println(Math.sqrt(9));	
      ************************************
      3.141592653589793
      10
      10
      13.0
      13.0
      12.0
      12.0
      100
      8.0
      0.13898377596160916
      12
      13
      3.0

14.12_常见对象(Random类的概述和方法使用)

  • A:Random类的概述
    • 此类用于产生伪随机数(通过算法利用种子算出来的随机数)。要先创建对象再调用方法生成随机数
    • 如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
  • B:构造方法
    • public Random()//默认种子:为当前时间的毫秒值
    • public Random(long seed)//带种子的构造方法
  • C:成员方法
    • public int nextInt()//调用nextInt()生成随机数在int的取值范围内
    • public int nextInt(int n)(重点掌握)//生成随机数在0~指定数之间(不包括制定数)
    • 				//Random r = new Random();//
      				Random r = new Random(1111);//通过种子算出随机数。执行程序每次生成随机数一样因为种子是一样的
      		
      				for(int i = 0; i < 10; i++) {
      					//System.out.println(r.nextInt());//调用nextInt()生成随机数在int的取值范围内
      					System.out.println(r.nextInt(10));//生成随机数在0~指定数之间(不包括制定数)
      **************************************
      6 6 5 9 0 7 5 4 8 6 

14.13_常见对象(System类的概述和方法使用)

  • A:System类的概述
    • System 类包含一些有用的类字段和方法。它不能被实例化。
  • B:成员方法
    • public static void gc()//运行垃圾回收器
    • public static void exit(int status)//退出jvm虚拟机, System.exit(0); 之后的代码就不会执行
    • public static long currentTimeMillis()//返回以毫秒为单位的当前时间
  • C:案例演示
    • System类的成员方法使用
    • 		demo1();
      		demo2();
      		long start = System.currentTimeMillis();
      		for(int i = 0; i < 100000; i++) {
      			System.out.print("*");
      		}
      		long end = System.currentTimeMillis();//返回以毫秒为单位的当前时间
      		System.out.println(end - start);//计算一个程序执行时间
      	}
      
      	public static void demo2() {
      		Person p1 = new Person("张三", 23);
      		System.out.println(p1);
      		
      		System.exit(0);	//退出jvm虚拟机,	System.exit(0);	之后的代码就不会执行
      		Person p2 = new Person("李四", 24);
      		System.out.println(p2);
      	}
      
      	public static void demo1() {
      		Person p = new Person("张三", 23);
      		System.out.println(p);
      		p = null;
      		System.gc();		//启动垃圾回收
      	}
      *************************************
      Person [name=张三, age=23]
      Person [name=张三, age=23]
      垃圾被回收了

14.14_常见对象(BigInteger类的概述和方法使用)

  • A:BigInteger的概述
    • 可以让超过Integer范围内的数据进行运算(用于计算更大的整数)
  • B:构造方法
    • public BigInteger(String val)
  • C:成员方法
    • 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)//商和余数
    • 		BigInteger b1 = new BigInteger("100");
      		BigInteger b2 = new BigInteger("2");
      		System.out.println(b1.add(b2));	//+
      		System.out.println(b1.subtract(b2));//-
      		System.out.println(b1.multiply(b2));  //*
      		System.out.println(b1.divide(b2));  //除
      		BigInteger[] arr = b1.divideAndRemainder(b2);	//商和余数
      		System.out.println(arr[0]);
      		System.out.println(arr[1]);
      *********************************
      102
      98
      200
      50
      50
      0

14.15_常见对象(BigDecimal类的概述和方法使用)

  • A:BigDecimal的概述(更精确的计算小数)

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

    • 不可变的、任意精度的有符号十进制数。

  • B:构造方法
    • public BigDecimal(String val)
  • C:成员方法
    • public BigDecimal add(BigDecimal augend)
    • public BigDecimal subtract(BigDecimal subtrahend)
    • public BigDecimal multiply(BigDecimal multiplicand)
    • public BigDecimal divide(BigDecimal divisor)
  • D:案例演示
    • BigDecimal类的构造方法和成员方法使用
    • 		/*BigDecimal b1 = new BigDecimal(2.0);	//创建BigDecimal对象,用double数当作参数可以,但是开发不用
      		BigDecimal b2 = new BigDecimal(1.1);
      		
      		System.out.println(b1.subtract(b2));
      		BigDecimal b1 = new BigDecimal("2.0");	//创建BigDecimal对象,推荐使用	,参数是字符串
      		BigDecimal b2 = new BigDecimal("1.1");
      		System.out.println(b1.subtract(b2));*/
      		
      		BigDecimal b1 = BigDecimal.valueOf(2.0);//创建BigDecimal对象,推荐使用,参数是double数
      		BigDecimal b2 = BigDecimal.valueOf(1.1);
      		System.out.println(b1.subtract(b2));
      ************************************
      0.9

14.16_常见对象(Date类的概述和方法使用)

  • A:Date类的概述(在java.util包下)
    • 类 Date 表示特定的瞬间,精确到毫秒。
  • B:构造方法
    • public Date()
    • public Date(long date)
  • C:成员方法
    • public long getTime() 获取当前时间的毫秒值
    • public void setTime(long time) 设置毫秒值
    • 		demo1();
      		Date d1 = new Date();
      		System.out.println(d1);
      		d1.setTime(1000);//									//设置毫秒值
      		System.out.println(d1.getTime());	//				//获取当前时间对象的毫秒值
      		System.out.println(System.currentTimeMillis());
      	}
      
      	public static void demo1() {
      		Date d1 = new Date();	//创建当前时间对象
      		System.out.println(d1);
      		Date d2 = new Date(2000);//根据指定的毫秒值创建时间对象,即创造出的时间对象的毫秒值为指定数。(1000毫秒等于1秒)
      		System.out.println(d2);
      	}
      
      }
      **************************************
      Thu Apr 13 17:19:08 GMT+08:00 2017
      Thu Jan 01 08:00:02 GMT+08:00 1970
      Thu Apr 13 17:19:08 GMT+08:00 2017
      1000
      1492075148480

14.17_常见对象(SimpleDateFormat类实现日期和字符串的相互转换)

  • A:DateFormat类的概述
    • DateFormat 是日期/时间格式化子类的抽象类(不能直接创建对象),它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat
  • B:SimpleDateFormat构造方法
    • public SimpleDateFormat()
    • public SimpleDateFormat(String pattern)
  • C:成员方法
    • public final String format(Date date)//将给定的 Date 对象格式化为日期/时间字符串。
    • public Date parse(String source)//解析字符串的文本,生成 Date对象
    • 		demo1();
      		String str = "2012年3月22日 08:30:30";
      		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
      		Date d = sdf.parse(str);//将日期字符串转换成日期对象
      		System.out.println(d);
      	}
      
      	public static void demo1() {
      		Date d = new Date();//创建日期对象
      		DateFormat df = DateFormat.getDateInstance();	//返回DateFormat的子类对象
      		/*DateFormat是个抽象类,不能直接new得一个对象
      		 * getDateInstance()的底层:是创建了一个子类SimpleDateFormat对象
      		 * 父类引用返回一个子类对象
      		 */
      		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//指定日期格式
      		
      		System.out.println(d);
      		System.out.println(df.format(d));//对日期对象格式化
      		String time = sdf.format(d);
      		System.out.println(time);		//format()方法继承自DateFormat
      	}
      ***********************************
      Thu Apr 13 17:33:06 GMT+08:00 2017
      2017-4-13
      2017年04月13日 17:33:06
      Thu Mar 22 08:30:30 GMT+08:00 2012

14.18_常见对象(你来到这个世界多少天案例)

  • A:案例演示
    • 需求:算一下你来到这个世界多少天?

14.19_常见对象(日期工具类的编写和测试案例)

  • A:案例演示
  • 日期工具类:将时间对象获取时间字符串的方法和时间字符串获取时间对象方法进行封装
    • 日期工具类的编写
    • 日期工具类的测试
    • 	public class Demo08_DateUtil {
      	private Demo08_DateUtil(){}	//私有构造函数,不让其他类创建本类对象,直接类名.调用
      	/*
      	 * 根据时间对象获取时间字符串
      	 * 1,返回值类型String
      	 * 2,参数列表Date d,String format//指定格式
      	 */
      	public static String dateToString(Date d,String format) {
      		SimpleDateFormat sdf = new SimpleDateFormat(format);
      		return sdf.format(d);
      	}
      	
      	/*
      	 * 根据时间字符串获取时间对象
      	 * 1,返回值类型Date
      	 * 2,参数列表,String time,String format
      	 */
      	public static Date stringToDate(String time, String format) throws ParseException {
      		SimpleDateFormat sdf = new SimpleDateFormat(format);
      		return sdf.parse(time);
      	}
      }
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      public class Demo09_DateFormat {//该类中调用Demo08_DateFormat类的方法完成:Date类和String类的相互转换
      
      
      	/**
      	 * @param args
      	 * @throws ParseException 
      	 */
      	public static void main(String[] args) throws ParseException {
      		demo1();
      		String time = "2008年08月08日";
      		Date d = Demo08_DateUtil.stringToDate(time, "yyyy年MM月dd日");
      		System.out.println(d);
      	}
      
      	public static void demo1() {
      		Date d = new Date();
      		String time = Demo08_DateUtil.dateToString(d, "yyyy年MM月dd日");
      		System.out.println(time);
      	}
      
      }

14.20_常见对象(Calendar类的概述和获取日期的方法)

  • A:Calendar类的概述
    • Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAYOFMONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
    • Calendar类称为日历类,是个抽象类,不能直接创建对象
  • B:成员方法
    • public static Calendar getInstance()
    • public int get(int field)

14.21_常见对象(Calendar类的add()和set()方法)

  • A:成员方法
    • public void add(int field,int amount)//对日历字段进行修改
    • public final void set(int year,int month,int date)//设置年月日
  • B:案例演示
    • Calendar类的成员方法使用
    • 		demo1();
      		Calendar c = Calendar.getInstance();
      		//c.add(Calendar.MONTH, -3);//(int field,int amount)amout变量对前面的日期字段增加或减少(根据正负数)
      		//c.set(Calendar.YEAR, 2000);//设置年为2000
      		c.set(2008, 12, 8);//月份中给的数:实际月份应该是给的数+1
      		System.out.println(c.get(Calendar.YEAR));
      		System.out.println(c.get(Calendar.MONTH) + 1);//c.get(Calendar.MONTH) + 1获取真实的月份
      		System.out.println(c.get(Calendar.DAY_OF_MONTH));
      	}
      
      	public static void demo1() {//Calendar日历类,是一个抽象类,不能直接创建对象
      		Calendar c = Calendar.getInstance();//getInstance()获取 Calendar实例的方法,使用默认时区和语言环境获得一个日历。
      		int year = c.get(Calendar.YEAR);//通过指定的字段获取日期值
      		System.out.println(year);//YEAR,MONTH...是Calendar的字段。所以使用时要通过Calendar调用
      		int month = c.get(Calendar.MONTH);
      		System.out.println(month + 1);//get(Calendar.MONTH)表示获取月份,返回值是0~11。所以实际月份:返回值+1
      		int day = c.get(Calendar.DAY_OF_MONTH);
      		System.out.println(day);
      	}
      
      **************************************
      2017
      4
      13
      2009
      1
      8
      

      注:Calendar类的方法:
    • getInstance():获取Calendar实例
    • get(int field):通过指定的字段field获取日期值

14.22_常见对象(如何获取任意年份的2月份有多少天)

  • A:案例演示
    • 需求:键盘录入任意一个年份,获取任意一年的二月有多少天
    • 	System.out.println(getNumber(2012));
      		
      	}
      	
      	/*
      	 * 获取某年的2月多少天
      	 * 1,返回值类型,int类型
      	 * 2,参数列表,int year
      	 */
      	public static int getNumber(int year) {
      		Calendar c = Calendar.getInstance();
      		c.set(year, 2, 1);						//设置那一年的3月1日
      		c.add(Calendar.DAY_OF_MONTH, -1);		//把3月1日向前减去一天
      		return c.get(Calendar.DAY_OF_MONTH);	//获取那年的2月多少天
      	}
      	
      	/*
      	 * 根据给定的年判断是闰年还是平年
      	 * 1,返回值类型boolean
      	 * 2,参数列表,int year
      	 */
      	public static boolean getYear(int year) {
      		Calendar c = Calendar.getInstance();
      		c.set(year, 2, 1);						//设置那一年的3月1日
      		c.add(Calendar.DAY_OF_MONTH, -1);		//把3月1日向前减去一天就可以得到2月的最后一天
      		return c.get(Calendar.DAY_OF_MONTH) == 29;
      	}
      }
      


14.23_day14总结

  • 把今天的知识点总结一遍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值