//SimpleDateFormat类的使用:
package exercise3; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class test { public static void main(String[] args) throws ParseException { //获取当前系统时间,格式为: Thu Nov 01 18:27:06 CST 2018 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //format: 按指定的目标格式把Date对象转换为String String dateFormat = sdf.format(date); //parse: 按指定的源格式把String转换为Date对象 Date dateFormatParse = sdf.parse(dateFormat); System.out.println("date: "+date); System.out.println("date.toString: "+date.toString()); System.out.println("dateFormat: "+dateFormat); System.out.println("dateFormatParse "+dateFormatParse); } }
//返回一个值,该值指示指定的 String 对象是否出现在此字符串中。
//String类的contains方法:
package task02; public class test { public static void main(String[] args) { String source = "A B C D"; if(source.contains("A")){ System.out.println("Y"); }else{ System.out.println("N");//N } } }
//其实List集合中也有这个API 用是用equals来比较,另外要重写equals,即比较的是对象的内容
package Exercise.exercise; public class User { private String name; private String pswd; public User(String name, String pswd) { this.name = name; this.pswd = pswd; } public User() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPswd() { return pswd; } public void setPswd(String pswd) { this.pswd = pswd; } @Override public String toString() { return "User [name=" + name + ", pswd=" + pswd + "]"; } public boolean equals(Object obj) { if(this==obj)//地址相同 return true; else if(obj!=null&&obj instanceof User) { User u=(User)obj; return this.name.equals(u.name)&&this.pswd.equals(u.pswd); } else return false; } } package Exercise.exercise; import java.util.ArrayList; import java.util.Scanner; /*3.创建一个User类,包含name和password两个成员变量. * 向一个ArrayList对象添加5个User对象, * 这5个User对象的成员变量的值分别为name="1",password="a", * name="2",password="b", * name="3",password="c", * name="4",password="d", * name="5",password="e"。 * 然后创建一个User对象,这个User对象的成员变量为name="3",password="c", * 然后使用contains方法测试这个对象是否包含在ArrayList对象中。 * */ public class Work2 { public static void main(String[] args) { ArrayList<User> list = new ArrayList<>(); Scanner sc = new Scanner(System.in); for(int i=0;i<5;i++){ System.out.println("请输入姓名"); String name = sc.next(); System.out.println("请输入密码"); String pswd = sc.next(); User user = new User(name,pswd); list.add(user); } for(int i=0;i<list.size();i++){ User user = list.get(i); System.out.println(user); } User u = new User("5","e"); Boolean f = list.contains(u); if(f){ System.out.println("包含"); }else{ System.out.println("不包含"); } } }
Java Character.isDigit() 方法,判断字符是否为数字
Digit[ˈdɪdʒɪt] 数字,拇指;
语法: boolean isDigit(char ch)
入参: 字符 ;
返回值: true/false;
eg:
public class Test { public static void main(String args[]) { System.out.println(Character.isDigit('a')); System.out.println(Character.isDigit('9')); } } //false //true
java.lang.String.getBytes(Charset charset) :按给定的字符集的字节序列将此String解码,并将结果存到一个新的字节数组。
package test; import java.lang.*; public class StringDemo { public static void main(String[] args) { try { String str1 = "hello"; System.out.println("string1 = " + str1); byte[] arr = str1.getBytes("utf-8");//按照utf-8的编码格式将str1进行解码; 并返回一个字节数组; System.out.println("arr:"+arr);//按照utf-8进行解码,并返回字节数组; String str2 = new String(arr); System.out.println("new string = " + str2); } catch(Exception e) { System.out.print(e.toString()); } } }
string1 = hello
arr:[B@1b6d3586
new string = hello