013.day13

复习字符串内容

1.字符串的加号

// TODO 字符串复习:字符串处理方法执行后返回的是一个新的字符串
		String s1 = "123";
		String s2 = "1";
		String s3 = "23";
		int s4 = 1;
		int s5 = 23;
		System.out.println(s1 == (s2 + s3));//false
		System.out.println(s1 == s2 + "23");//false
		System.out.println(s1 == s2 + s5);//false
		System.out.println(s1 == "1" + "23");//true
		// 字面常量存储在常量池中
		System.out.println(s1 == 1 + "23");//true
		
		String s = "abc/sss/dde";
		System.out.println(s.substring(s.indexOf("/") + 1, s.lastIndexOf("/")));
		// 当字符串中没有匹配到分割点则原字符串以数组的形式返回,下标为0
		System.out.println(s.split("t").length);
		String s6 = "    6 5  ";
		System.out.println(s6.trim());

2.字符串类型之间的相互转换

字符串类型之间的相互转换:通过构造方法

// String -> StringBuffer/StringBuilder
		String s = "123";
		// 将s转换成stringBuffer类型
		StringBuffer stringBuffer = new StringBuffer(s);
		System.out.println(stringBuffer);
		stringBuffer.append("456");
		// 在后面追加"456",改变了原stringBuffer
		System.out.println(stringBuffer);// 123456
		// StringBuffer/StringBuilder -> String
		s = new String(stringBuffer);
		System.out.println(s);

3.StringBuffer常用方法

// TODO StringBuffer常用方法:直接作用在字符串本身,不需要重新赋值
		StringBuffer stringBuffer = new StringBuffer("123");
		// 字符串的追加
		stringBuffer.append("456");
		System.out.println(stringBuffer);
		// 使用append方法时需要将一个字符串转换为StringBuffer
		new StringBuffer("456").append(stringBuffer);
		// 使用加号可以将字符串与stringBuffer直接拼接,底层调用了tostring()
		System.out.println("456" + stringBuffer);

4.字符序列的删除

// TODO 字符序列的删除
		StringBuffer stringBuffer = new StringBuffer("sss/sdad/sss");
		// 根据标识符删除某一段字符串(包括标识符本身),在结束位置加1(左闭右开)
		stringBuffer.delete(stringBuffer.indexOf("/"), stringBuffer.lastIndexOf("/") + 1);
		System.out.println(stringBuffer);// ssssss

5.移除单个字符

// TODO 移除单个字符
		StringBuffer stringBuffer = new StringBuffer("asd?asda");
		// 删除指定位置的单个字符
		stringBuffer.deleteCharAt(stringBuffer.indexOf("?"));
		System.out.println(stringBuffer);//asdasda

6.字符序列复制

// TODO 字符序列复制
		StringBuffer stringBuffer = new StringBuffer("abcdefg");
		char[] c = new char[stringBuffer.length()];
		// 将字符(stringBuffer)从此序列(0, 3)复制到目标字符数组(c),从c的索引2开始粘贴
		stringBuffer.getChars(0, 3, c, 2);
		// 保证要复制的字符串长度小于等于粘贴的字符数组
		System.out.println(c);//  abc(有两个空格)
		System.out.println(stringBuffer);//abcdefg

7.insert插入

// TODO insert插入(插入位置,插入内容)
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(12);
		stringBuffer.append(45);
		System.out.println(stringBuffer);//1245
		// 将所指定的内容插入到当前索引的前一个位置
	    stringBuffer.insert(2, "3333");
		System.out.println(stringBuffer);//123333345
		//char[] c = new char[] { '6' };
		String b = new String("11");
		// 第一个参数为插入的位置,第二个参数为数据来源(字符数组),第三个参数为数据来源开始复制的头,第四个参数为尾
		stringBuffer.insert(2, b, 0, b.length());
		System.out.println(stringBuffer);//1211333345

8.字符串替换

// TODO 字符串替换
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("1258");
		stringBuffer.append("3325");
		// 指定四个字符长度的区间,起始位置包括当前的下标,终止位置不包括当前的下标
		stringBuffer.replace(2, 6, "4");
		System.out.println(stringBuffer);

9.字符串反转

// TODO 字符串反转
		StringBuffer stringBuffer = new StringBuffer("123456");
		System.out.println(stringBuffer);//123456
		// 直接将字符串进行反转,作用在自身
		stringBuffer.reverse();
		System.out.println(stringBuffer);//654321

今日学习内容

1.Date工具类

  • 1)日期类型
// TODO 日期类型
		// 1.无参构造方法->获得当前的时间
		Date date = new Date();
		System.out.println(date);//Mon Jul 30 19:28:15 CST 2018
		
		// 2.有参构造方法(参数为long类型时间戳)
		System.out.println(new Date(date.getTime()));
		// 输出一个时间戳
		System.out.println(new Date().getTime());//1532950363639
		// 月/日/年 时间 AM/PM
		// 3.过时方法,必须传入指定格式的字符串,才能够正确解析时间
		System.out.println(new Date("07/30/2018 10:00:00 PM"));
  • 2)Date类型的方法
// TODO Date类型的方法
		Date date = new Date();
		// 获得当前时间的时间戳
		date.getTime();
		// 使用一个时间戳设置时间
		date.setTime(date.getTime());
		
		Date date2 = new Date("07/30/2018 10:00:00");
		Date date3 = new Date("07/29/2018 10:00:00");
		// 比较日期的先后,返回值类型为布尔类型
		System.out.println(date2.before(date3));//false
		System.out.println(date2.after(date3));//true
		// compareTo:相等返回0,参数大返回-1,参数小返回1
		System.out.println(date2.compareTo(date3));//1

2.Calendar工具类

  • 1)Calendar对象构建
// TODO Calendar对象构建
		Calendar calendar = Calendar.getInstance();//获得一个日历实例的方式(Calendar是一个抽象类)
		System.out.println(calendar.get(Calendar.YEAR));//2018 获得当前的年份
		// 月份从0开始,0代表一月份,获取月份时加1
		System.out.println(calendar.get(Calendar.MONTH) + 1);//7
		// 把星期日当做一周开始的第一天,0代表周日,需要手动处理
		System.out.println(calendar.get(Calendar.DAY_OF_WEEK) - 1);
		/*
		    YEAR 当前的年份
			MONTH 当前的月份
	 		WEEK_OF_YEAR 一年中的第几周
	 		WEEK_OF_MONTH 一个月的第几周
	 		DATE 一个月中的第几天
	 		DAY_OF_MONTH 一个月中的第几天
	 		DAY_OF_YEAR 一年中的第几天
	 		DAY_OF_WEEK 一周中的第几天 -> 星期几
	 		DAY_OF_WEEK_IN_MONTH 当前月中的第几个星期
		 */
		System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
  • 2)Calendar常用方法
// TODO Calendar常用方法
		Calendar calendar = Calendar.getInstance();
		// 获取某个属性的最大值-大小月,是否是闰年
		System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));//31
		System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH));//所有月都为31天,上面这个方法才是正确的(有些月只有28天等)
		//给这个日历对象的当前月设为2月
		calendar.set(Calendar.MONTH, 2);
		System.out.println(calendar.get(Calendar.MONTH));//2
		
		// getMaximum用于获取某个属性的最大值,与当前时间无关
		// getActualMaximum用于获取当前时间的最大值
		System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH));
		System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  • 3)时间设置
// TODO 时间设置
		Calendar calendar = Calendar.getInstance();
		// 通用设置,第一个参数代表配置项,第二个参数代表设置的时间的值
		calendar.set(Calendar.HOUR_OF_DAY, 15);
		System.out.println(calendar.get(Calendar.HOUR_OF_DAY));//15
		// 年月日
		calendar.set(2018, 9, 10);
		System.out.println(calendar.get(Calendar.YEAR) + " " +
				calendar.get(Calendar.MONTH) + " " + calendar.get(Calendar.DATE));//2018 9 10
		// 年月日时分
		calendar.set(2018, 9, 10, 12, 50);
		System.out.println(calendar.get(Calendar.YEAR) + " " +
				calendar.get(Calendar.MONTH) + " " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.HOUR_OF_DAY)
				 + " " + calendar.get(Calendar.MINUTE));//2018 9 10 12 50
		// 年月日时分秒
		calendar.set(2018, 9, 10, 12, 50, 20);//同上面一个道理设置年月日时分秒
  • 4)calendar和Date的相互转换
// TODO calendar和Date的相互转换
		Calendar calendar = Calendar.getInstance();
		calendar.set(2009, 4, 6, 10, 10, 10);//设置时间
		// 将Calendar转换为Date
		Date date = calendar.getTime();
		System.out.println(date);//Tue May 06 10:10:10 CST 2008
		// 将Date转换为Calendar
		calendar.setTime(date);
		calendar.set(Calendar.MONTH, 1);//设置月份为2月
		System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));//28天
  • 判断闰年练习
// TODO 判断闰年练习
		getByDayOfYear();
		getByDayOfMonth();
	}

	/**
	 * 通过判断一年是否是366天来判断闰年
	 */
	public static void getByDayOfYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, 1900);
		for (int i = 1900; i <= 2000;) {
			if (calendar.getActualMaximum(Calendar.DAY_OF_YEAR) == 366) {
				System.out.println(calendar.get(Calendar.YEAR));
			}
			calendar.set(Calendar.YEAR, ++i);
		}
	}

	/**
	 * 通过判断一年的二月是否是29天来判断闰年
	 */
	public static void getByDayOfMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(1900, 2, 1);
		for (int i = 1900; i <= 2000;) {
			// add方法可以用于做日期的计算,增加或减少
			calendar.add(Calendar.DAY_OF_YEAR, -1);
			if (calendar.get(Calendar.DAY_OF_MONTH) == 29) {
				System.out.println(calendar.get(Calendar.YEAR));
			}
			calendar.set(++i, 2, 1);
		}
	}

3.SimpleDateFormat

// TODO 日期的解析与格式化
		// 日期的解析:文本(字符串)转换成日期类型
		String dateStr = "2018-10-01 05:30:20";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date1 = format.parse(dateStr);
		System.out.println(date1);
		// 日期的格式化:将一个日期类型转换为可读性较好的字符串
		Date date2 = new Date();
		dateStr = format.format(date2);
		System.out.println(format.toPattern());
		System.out.println(dateStr);

4.Math工具类

  • 常用方法
// TODO Math类中的常量-常用方法
		// Math.PI -> 圆周率
		System.out.println(Math.PI);
		// Math.E -> 自然对数的底数
		System.out.println(Math.E);
		// 取绝对值abs()
		System.out.println(Math.abs(-200));
		// 指数运算
		System.out.println(Math.pow(2, 3));
		System.out.println(Math.pow(4, (double)1/2));
		System.out.println(Math.pow(8, (double)1/3));
		// 平方根
		System.out.println(Math.sqrt(4));
		// 立方根
		System.out.println(Math.cbrt(8));
		// 取较大值/较小值
		System.out.println(Math.max(25, -3));
		System.out.println(Math.min(41, 25));
		// 对数运算
		System.out.println(Math.log10(100));
		// 小数进位 -> 四舍五入
		System.out.println(Math.round(3.5));
		System.out.println(Math.round(3.4));
  • 随机数
// TODO Math中的随机数
		// random用于产生一个0~1之间的小数
		double d = Math.random();
		System.out.println(d);//0.41760308237593746
		// 获取某一区间的随机数
		// 1.确定随机数的区间=右-左+1 
		// 2.调整初始位置:加上左区间的值
		int a = (int)(Math.random()*(19 - 13 + 1) + 13);
		System.out.println(a);

5.Random工具类

  • 常规使用
		// 常规实例化方式
		Random random = new Random();
		// 无参时根据相应的数据类型返回对应范围内的随机数
		System.out.println(random.nextInt());
		// 方法中的参数为数据的区间长度
		// 在生成随机数后可以调整范围
		System.out.println(random.nextInt(10) + 5);//范围 5~15
		// 支持生成int,boolean,float,byte[] -> 生成一组随机数,double
		byte[] bs = new byte[5];
		// 生成一组byte范围内的数
		random.nextBytes(bs);
		for (byte b : bs) {
			System.out.println(b);
		}
  • 种子
// TODO Random的seed
		// 1.初始化对象时直接指定seed
		// 指定seed后,同一个seed生成的数字是稳定的
		Random random1 = new Random();
		// 每次生成的均是随机数
		System.out.println(random1.nextInt(10));
		System.out.println(random1.nextInt(10));
		// 参数为种子
		Random random2 = new Random(10);
		// 单次生成的随机数保持稳定
		System.out.println(random2.nextInt(10));
		System.out.println(random2.nextInt(10));
		System.out.println(random2.nextInt(10));
		// 2.通过方法指定或修改seed
		random1.setSeed(10);
		System.out.println(random1.nextInt(10));
		System.out.println(random1.nextInt(10));
		System.out.println(random1.nextInt(10));
		// seed相同时生成的数据顺序和值完全一致

6.Runtime

// TODO Runtime的使用
		Runtime runtime = Runtime.getRuntime();
		// 在Windows中相当于在运行中执行命令
		// /c执行完成之后关闭窗口
		// /k执行完成之后保留窗口
		// &&可以同时执行两个命令
		// SSH方式远程登录到Linux系统再执行相应的命令
		runtime.exec("cmd.exe /k start cd ../ && dir");

7.System

		// 1.获得系统当前时间
		System.out.println(System.currentTimeMillis());
		// 2.强制结束jvm
		// 参数是程序结束时的状态,shell脚本获取状态码$? == 0
		// System.exit(0);
		// 3.垃圾回收方法:提醒jvm进行垃圾回收
		String a = "1";
		String b = "2";
		String c = "3";
		System.gc();
		c = a;
		System.gc();
		c = b;
		System.out.println(c);
posted @ 2018-07-30 20:50  Yokiia  阅读(132)  评论(0编辑  收藏  举报