实用类(二)
1. ⽤户登录注册例⼦
package com.user; import java.util.Scanner; //用户名长度不小于3 //密码长度不小于6 //注册时两次输入密码 //必须相同 public class Register { public static void main(String[] args) { String name; String password1; String password2; Scanner sc =new Scanner(System.in); System.out.println("***欢迎进入注册系统***"); System.out.println(); System.out.println(); boolean flag=true; do { System.out.print("请输入用户名:"); name =sc.next(); System.out.print("请输入密码:"); password1 =sc.next(); System.out.print("请重新输入密码:"); password2 =sc.next(); if (name.length()<3||password1.length()<6){ System.out.println("用户名长度不小于3位,密码长度不小于6位"); }else if (!password1.equalsIgnoreCase(password2)){ System.out.println("两次输入的密码必须一致"); }else { System.out.println("注册成功!!!请牢记用户名和密码。"); flag=false; } }while (flag); } }
2. String 字符串的常⽤⽅法
package com.string; import java.util.Arrays; //字符串的常用方法 public class Demo01 { public static void main(String[] args) { //创建一个字符串 // String s1 ="This is an apple"; // String s2 =new String("This is an apple"); // String s3="This is an apple"; // String s4= new String("This is an apple"); // System.out.println(s1); // System.out.println(s2); // //求字符串的长度 length() // System.out.println(s1.length()); // System.out.println(s2.length()); // //判断两个字符串是否相等 // //== 地址 // System.out.println(s1==s2); // System.out.println(s1==s3); // System.out.println(s2==s4); // //equals比较的是内容 // System.out.println(s1.equals(s2)); // //不区分大小写比较两个字符串是否相等 // String a =new String("abc"); // String a1 =new String("ABC"); // System.out.println(a.equalsIgnoreCase(a1)); // //字符串大小写转换 // String s11 =s1.toUpperCase(); // System.out.println(s11); // String s12 = s11.toLowerCase(); // System.out.println(s12); // //连接字符串 // String t1= new String("abc"); // String t2= new String("def"); // String t3 =t1+t1; // System.out.println(t3); // String t4 =t1.concat(t2); // System.out.println(t4); // //查找搜索字符串(从左向右查找)如果在返回下标,不在返回-1 // String r1 =new String("abcdefghijk"); // String r2 =new String("ab"); // String r3 =new String("ok"); // System.out.println(r1.indexOf(r2)); // System.out.println(r1.indexOf(99));//c 99 a 97 b 98 // System.out.println(r1.indexOf(r2,5)); //查找搜索字符串(从右向左查找) // String r1 =new String("abcdefghijk"); // String r2 =new String("ab"); // String r3 =new String("ok"); // System.out.println(r1.lastIndexOf(r2)); // System.out.println(r1.lastIndexOf(r2,5));//从下标为5的位置往左查 //提取字符串 // String r1 =new String("abcdefghijk"); // String r2 =r1.substring(5); // System.out.println(r2); // String r3 =r1.substring(5,6);//从下标为5截取到下标为6但不包含下标为6的字符 // System.out.println(r3); //取出首尾空白 // String s1 = " \t abc \n "; // String s2 = s1.trim(); // System.out.println("s1的长度为:"+s1.length()+":"+s1); // System.out.println("s2的长度为:"+s2.length()+":"+s2); //判断字符串的开头或结尾 String s1 =new String("abcdefghijkabc"); System.out.println(s1.startsWith("ab")); System.out.println(s1.endsWith("bc")); //判断字符串是否包含子串 System.out.println(s1.contains("abcd")); System.out.println(s1.contains("bdce")); //拆分字符串 String s2 =new String("aa bb cc dd"); String[] split = s2.split(""); System.out.println(Arrays.toString(split)); //替换字符串 String s3 =new String("aabbccdeffgghjhj"); String replace =s3.replace("a","x"); System.out.println(replace); String s4 =new String("aaaa"); System.out.println(s4.replace("aa","b")); System.out.println(s4.replaceAll("aa","b")); } }
3. StringBuffer
package com.stringbuffer; //StringBuffer public class Demo02 { public static void main(String[] args) { //创建字符串 StringBuffer buff1 = new StringBuffer(); StringBuffer buff2 = new StringBuffer("abcdefg"); //追加字符串 StringBuffer buff3 = buff1.append(buff2).append(100).append(1.2); System.out.println(buff1); System.out.println(buff2); System.out.println(buff3); //插入字符串 StringBuffer s1 = new StringBuffer("123"); s1.insert(0, "aaa").insert(0, "bbb"); System.out.println(s1); //修改字符串[x,y)前闭后开 s1.replace(1, 4,"12345"); System.out.println(s1); s1.replace(6, 9, "xyz"); System.out.println(s1); //删除字符串[x,y)前闭后开 StringBuffer s3 = new StringBuffer("abcdefg"); s3.delete(3, 6); System.out.println(s3); //反转字符串 StringBuffer s4 = new StringBuffer("123456"); s4.reverse(); System.out.println(s4); //从右向左没3个加一个"," StringBuffer s5 = new StringBuffer("12345678"); for (int i = s5.length() - 3; i > 0; i -= 3) { s5.insert(i, ","); } System.out.println(s5); } }
5. java.util.Date类:表⽰⽇期和时间
package com.data; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DataTest { public static void main(String[] args) { //创建当前日期时间 Date now = new Date(); System.out.println(now); //获取表示时间的整数,单位是毫秒 long t =now.getTime(); System.out.println(t); System.out.println(System.currentTimeMillis()); //创建指定的日期时间 Date date =new Date(now.getTime()+24*60*60*1000); System.out.println(date); //格式化日期时间 SimpleDateFormat sdf =new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String s1 =sdf.format(now); String s2 =sdf.format(date); System.out.println(s1); System.out.println(s2); //把字符串转化为日期时间 String s3 =new String("2023-09-26 12:12:12"); System.out.println(s3); SimpleDateFormat sdf2 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = null; try { date1 = sdf2.parse(s3); System.out.println(date1); } catch (ParseException e) { e.printStackTrace(); } } }
6. Calendar类
package com.data; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class CalendarTest { public static void main(String[] args) { //抽象类,所以要通过静态的方法获取实例,获取当前的日期时间 Calendar cal =Calendar.getInstance(); // System.out.println(cal); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH)+1;//月从0开始 int day=cal.get(Calendar.DAY_OF_MONTH); int hour=cal.get(Calendar.HOUR_OF_DAY); int minute=cal.get(Calendar.MINUTE); int second=cal.get(Calendar.SECOND); // System.out.println(year+","+month+","+day+","+hour+","+minute+","+second); //创建指定的日期时间 cal.set(2023,9-1,26,12,12,12); System.out.println(cal); year = cal.get(Calendar.YEAR); month = cal.get(Calendar.MONTH)+1;//月从0开始 day=cal.get(Calendar.DAY_OF_MONTH); hour=cal.get(Calendar.HOUR_OF_DAY); minute=cal.get(Calendar.MINUTE); second=cal.get(Calendar.SECOND); // System.out.println(year+","+month+","+day+","+hour+","+minute+","+second); //转化为Date Date date=cal.getTime(); System.out.println(date); SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String s2 = sdf.format(date); System.out.println(s2); //date转化为Calendar cal.setTime(date); //获取毫秒值 long timeInMillis = cal.getTimeInMillis(); System.out.println(timeInMillis); //计算日期时间 cal.add(Calendar.YEAR,-10); Date time = cal.getTime(); System.out.println(time); } }