目录
Math
System.out.println("PI的值:"+Math.PI); System.out.println("-10的绝对值:"+Math.abs(-10)); System.out.println("Math.random()="+Math.random()); System.out.println("Math.round(98.56)="+Math.round(98.568977d)); System.out.println("Math.round(88.56789)="+Math.round(88.56789*100)/100.0); System.out.println("Math.sqrt(2d)="+Math.sqrt(2d));
Random
//随机数 Random r = new Random(); int x = r.nextInt(100); System.out.println("x="+x);
Date
//创建了一个日期对象 Date date = new Date(); System.out.println("当前时间的毫秒:"+date.getTime()); System.out.println(date);
Calendar
Calendar c = Calendar.getInstance(); // Calendar c = new GregorianCalendar(); int year = c.get(Calendar.YEAR);//年 int month = c.get(Calendar.MONTH);//月 int day = c.get(Calendar.DAY_OF_MONTH);//日 int hour = c.get(Calendar.HOUR_OF_DAY);//时 int minute = c.get(Calendar.MINUTE);//分 int second = c.get(Calendar.SECOND);//秒 int millisecond = c.get(Calendar.MILLISECOND);//毫秒 //星期 String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; int week = c.get(Calendar.DAY_OF_WEEK); StringBuilder sb = new StringBuilder(); sb.append(year).append("年").append(month).append("月").append(day).append("日 ") .append(hour).append(":").append(minute).append(":").append(second).append(" ") .append(millisecond).append(" ").append(weeks[week]); System.out.println(sb.toString());
DateFormat
// DateFormat df = DateFormat.getDateInstance(); //参数(指定日期风格,指定语言环境) // DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINA); // DateFormat df = DateFormat.getTimeInstance(); DateFormat df = DateFormat.getTimeInstance(DateFormat.LONG, Locale.getDefault()); String nowDate = df.format(new Date()); System.out.println("当前日期为:"+nowDate);
SimpleDateFormat
//用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。 // SimpleDateFormat sdf = new SimpleDateFormat(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); String s = sdf.format(new Date()); System.out.println(s);
Comparator
/** * 人类 * 如果自定义类的对象要实现排序(比较),那么就要实现Comparable接口,并实现接口的 * compareTo方法 */ public class Person implements Comparable<Person>{ private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } //比较器的比较方法 //如果当前对象小于参数对象,返回-1 //如果当前对象等于参数对象,返回0 //如果当前对象大于参数对象,返回1 @Override public int compareTo(Person o) { //实现:定义对象之间的比较规则 if(o==null){ throw new NullPointerException(); } if(this.age<o.age){ return -1; }else if(this.age>o.age){ return 1; } return 0; } }
/** * 人类*/ public class Person2{ private String name; private int age; public Person2() { } public Person2(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
/** * 自定义的类比较器 */ public class Person2Comparator implements Comparator<Person2>{ @Override public int compare(Person2 o1, Person2 o2) { if(o1==null || o2==null){ throw new NullPointerException(); } if(o1.getAge()<o2.getAge()){ return -1; }else if(o1.getAge()>o2.getAge()){ return 1; } return 0; } }
//对象的排序 public class PersonSortDemo { public static void main(String[] args) { String[] names = {"张三","李四","王五","刘备","张飞","关羽"}; Person[] persons = { new Person("张三", 27), new Person("李四", 29), new Person("王五", 41), new Person("刘备", 33), new Person("张飞", 25), new Person("关羽", 30) }; Person2[] persons2 = { new Person2("张三", 27), new Person2("李四", 29), new Person2("王五", 41), new Person2("刘备", 33), new Person2("张飞", 25), new Person2("关羽", 30) }; Arrays.sort(names); System.out.println(Arrays.toString(names)); //自定义类对象数组排序 Arrays.sort(persons); System.out.println(Arrays.toString(persons)); //使用自定义的比较器 Arrays.sort(persons2, new Person2Comparator()); System.out.println(Arrays.toString(persons2)); } }
Clone
/** * 一个类要想实现克隆功能,必须: * 1、实现Cloneable接口,该接口是一个标记接口 * 2、重写Object类中的clone方法 */ public class Cat implements Cloneable{ private String name; private int age; public Cat(String name, int age) { super(); this.name = name; this.age = age; } public Cat() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Cat [name=" + name + ", age=" + age + "]"; } //重写Object的clone方法 @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
//要创建一组类似的对象,那么从性能上考虑,就可以使用克隆技术 public class Test { public static void main(String[] args) { Cat c = new Cat("张三",3); System.out.println("c="+c); try { Cat c1 = (Cat)c.clone(); System.out.println("c1="+c1); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
我是天王盖地虎的分割线