JavaSE_day32(日历类的常用方法,包装类的介绍以及自动装箱拆箱原理说明,Integer包装类的构造方法、常用方法以及String与int类型的转化,正则表达式校验QQ号,元音练习)

1 A.java

 * Calendar:日历,提供了一些操作年月日时的方法
 * 
 * 获取 -get
 * 修改 -set
 * 添加 -add

public class A {

	public static void main(String[] args) {
		//static Calendar getInstance()  
		Calendar c = Calendar.getInstance();
		
		//void set(int field, int value) :把指定的字段修改成指定的值
		c.set(Calendar.DAY_OF_MONTH, 20);
		
		
		//void add(int field, int amount): 在指定的字段上加上指定的值
		c.add(Calendar.YEAR, -1); //当前时间倒回去1年
		
		//int year = c.get(1);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		
		
		System.out.println(year + "年" + month + "月" + day + "日");
		 
	}
}

练习:

 * 第一题:分析以下需求,并用代码实现
    1.已知日期字符串:"2015-10-20",将该日期字符串转换为日期对象
    2.将(1)中的日期对象转换为日历类的对象
    3.根据日历对象获取该日期是星期几,以及使这一年的第几天   
    4.通过键盘录入日期字符串,格式(2015-10-20) 
            如输入:2015-10-20,输出"2015年-10月-20日是星期二,是2015年的第293天"

	    public static void main(String[] args) throws ParseException {
	    	// 获取scanner对象
	        Scanner sc = new Scanner(System.in);
	        System.out.println("请录入日期,格式(2015-10-20):");
	        // 定义字符串对象接收录入的日期
	        String s = sc.nextLine();
	        // 创建字符串解析对象
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        // 将该日期字符串转换为日期对象
	        Date date1 = sdf.parse(s);

	        // System.out.println(date1);
	        // 设置日历类对象
	        Calendar instance = Calendar.getInstance();
	        // 设置日期
	        instance.setTime(date1);
	        // 获取年月日天
	        int year = instance.get(Calendar.YEAR);
	        int month = instance.get(Calendar.MONTH);
	        int day = instance.get(Calendar.DAY_OF_MONTH);
	        int dayyear = instance.get(Calendar.DAY_OF_YEAR);
	        // 老外周日---周一,对应数字1-7
	        int week = instance.get(Calendar.DAY_OF_WEEK);

			// 因为索引是从0开始的,所以在前面添加一个空
	        String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
	        String wk = weeks[week];

	        System.out.println(year+"-年"+month+"-月"+day+"-日"+","+"是"+wk+","+"是"+year+"年的第"+dayyear+"天");
	    }
	

2 B.java

 * 需求:判断一个数是否符合int类型的范围[int类型的范围是 -2的31次方 到 2的31次方-1]
 * 
 * 由于基本数据类型只能做一些简单的操作和运算,所以Java为我们封装了基本数据类型,为每种基本数据类型提供了包装类
 * 包装类就是封装了基本数据类型的类,为我们提供了更多复杂的方法和一些变量
 * 
 * 
      byte        Byte
      short        Short
      char        Character
      int        Integer
      long        Long
      float        Float
      double    Double
      boolean    Boolean
 * 
 * Integer: int类型与String类型相互转化
 *         String ---> int
 *             方式: static int parseInt(String s) ---可以将字符串转成int类型
 *         int --- String
 *             方式1: + ""
 *             方式2:String toString() ----可以将Integer转成String类型
 * 
 * 构造方法:
 *         Integer(int value) --还是Integer类型的
 *         Integer(String s) --可以将字符串转成Integer类型
其他方法:

         方式:int intValue() 以int类型返回该Integer类型的值

小结:

        字符串变成整型:静态方法static int parseInt(String s) 、构造方法Integer(String s)

        整型变成字符串:String toString()

public class B {

	public static void main(String[] args) {
	/*	int n = 10;
		if(n >= Math.pow(-2, 31) && n <= Math.pow(2, 31) -1) {
			System.out.println("符合");
		}
		else {
			System.out.println("不符合");
		}
*/
		
		Integer i = new Integer("10");  //构造方法将字符串转成int类型!
		System.out.println(i*50);
		
	 
		int a = i.intValue();         	//以int类型返回该Integer的值
		System.out.println(a + 10 );
		
 
		int b = Integer.parseInt("20"); //方法将字符串转成int类型
		System.out.println(b + 30);
		
		
//		将int类型转成字符串的两种方法:
		
		//1.构造方法
		Integer i2 = new Integer(40); //这里的i2还是int类型的
		String s = i2.toString();
		System.out.println(s);
		
		//2.调用toString 
		String s2 = Integer.toString(50);
		System.out.println(s2);
		

		
	}
}

3 C.java

 * JDK1.5特性:自动装箱和拆箱
 * 装箱:基本数据类型转成包装类对象的过程
 * 拆箱:包装类对象转成基本数据类型的过程
 * 

public class C {

	public static void main(String[] args) {
		//Integer i = new Integer(10);
		
		//自动装箱:
		//Integer i = 10;   //相当于: Integer i = new Integer(10);  把基本数据类型赋值给引用数据类型就有了自动装箱
		
		
		//自动拆箱
		
		//Integer i = 10;   //相当于 int a = i.intValue();         	把引用数据类型给基本数据类型赋值就有了自动拆箱

		
		Integer i = 10;
		Integer i2 = 20;
		Integer i3 = i + i2;
		System.out.println(i3);
		/*
		 * Integer i3 = new Integer(i.intValue() + i2.intValue());
		 * 
		 */
		
		ArrayList list = new ArrayList();
		list.add(1);//自动装箱,list.add(new Integer(1));
	}
}

4 D.java

 *     校验QQ号码
*         要求必须是5-15位
*         0不能开头
*         必须都是数字
    
    正则表达式:就是一套规则,可以用于匹配字符串
    
  String类的一个方法:

          boolean matches(String regex) :判断当前字符串是否匹配指定的正则表达式,如果匹配则返回true,否则返回false


*     常用的正则表达式:

public class D {

	public static void main(String[] args) {
		String qq = "123485";
/*		boolean flag1 = checkQQ(qq);
		System.out.println(flag1);*/
		
		//第一个字符  , 其余字符规定范围和长度了
		boolean flag = qq.matches("[1-9][0-9]{4,14}");
		System.out.println(flag);
	}
	
	public static boolean checkQQ(String qq) {
		int length = qq.length();
		//要求必须是5-15位
		if(length < 5 || length > 15) {
			return false;
		}
		
		//0不能开头
		if(qq.startsWith("0")) {
			return false;
		}
		
		//必须都是数字
		for (int i = 0; i < length; i++) {
			//得到参数的每一个字符
			char c = qq.charAt(i);
			if(c < '0' || c > '9') {
				return false;
			}
		}
		return true;//符合要求
	}
}

5 E.java

 *  判断字符串”qaq”中间的字符是否是元音 
 * 元音? 
 *      aeiou
 *      AEIOU
 *      中间的字符是这些就是元音

public class E {

	public static void main(String[] args) {
		boolean flag = check2("1aq");
		System.out.println(flag);
	}
	
	
	public static boolean check(String s) {
		s = s.toLowerCase();// 转成小写
		
		char ch = s.charAt(1);  //拿字符串中间的字符取出来!
		/*if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
			return true;
		}
		
		return false;*/
		
		String str = "aeiou";
		
		return str.contains(ch + "");
	}
	
	//改进方法-正则表达式
	public static boolean check2(String s) {
		/*
		 * 
		 * 两边可以是任意字符[a-zA-Z_0-9][aeiouAEIOU][a-zA-Z_0-9]
		 * [a-zA-Z_0-9]可以使用预定义字符:\\w来表示
		 * 
		 * 
		 */
		return s.matches("\\w[aeiouAEIOU]\\w");
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值