Runtime类

封装了java命令本身的运行进程
java命令运行后,本身是多任务操作系统中的一个进程,在该进程中执行一个新的进程使用exec方法
打开了记事本

    public static void main(String[] args) {
        Runtime runtime=Runtime.getRuntime();
        try {
            runtime.exec("notepad.exe");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Date类,Calendar类,DateFormat类

GregorianCalendar,SimpleDateFormat

public static void main(String[] args) {
        //获取当前的GregorianCalender类对象
        Calendar calendar=Calendar.getInstance();
        //输出时间
       System.out.println(calendar.get(calendar.YEAR)+"年"+(calendar.get(calendar.MONTH)+1)+"月"
        +calendar.get(calendar.DAY_OF_MONTH)+"日"+calendar.get(calendar.HOUR_OF_DAY)+":"+calendar.get(calendar.MINUTE)
        +":"+calendar.get(calendar.SECOND));
        //天数增加20天
        calendar.add(calendar.DAY_OF_YEAR,20);
        //输出时间
        System.out.println(calendar.get(calendar.YEAR)+"年"+(calendar.get(calendar.MONTH)+1)+"月"
        +calendar.get(calendar.DAY_OF_MONTH)+"日"+calendar.get(calendar.HOUR_OF_DAY)+":"+calendar.get(calendar.MINUTE)
        +":"+calendar.get(calendar.SECOND));
        
    }

更改日期字符串

!!! 此处疑问:当dd为DD时,结果异常

public static void main(String[] args) {
        SimpleDateFormat d1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat d2=new SimpleDateFormat("yyyy年MM月dd日  hh时mm分ss秒");
        try {
            Date time=d1.parse("2020-03-20 13:26:23");
            System.out.println(d2.format(time));
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Math类和Random类

hashCode
key:改写,散列码

class Person{
    private String name;
    private int age;
    Person(String name1,int age1){
        this.name=name1;
        this.age=age1;
    }
    public String  toString() {
        return "姓名:"+this.name+"年纪:"+this.age;
    }
    //加入下列代码之前输出null,之后为"张三"
    public boolean equals(Object object) {
        return true;
    }
    public int hashCode() {
        return 20;
    }
}

public class HashCode {
    public static void main(String[] args) {
        HashMap hm=new HashMap();
        hm.put(new Person("张三",     23), "张三");
        System.out.println(hm.get(new Person("张三",     23)));
        
    }
    
    
}

对象克隆

class Employee implements Cloneable{
    private String name;
    private int age;
    public Employee(String name,int age) {
        this.name=name;
        this.age=age;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    @Override
    public String toString() {
        return "姓名:"+this.name+"年龄:"+this.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;
    }
}

public class CloneDemo {
    public static void main(String[] args) {
        Employee e1=new Employee("张桑,", 22);
        Employee e2=null;
        try {
            e2=(Employee) e1.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        e2.setName("李四,");
        e2.setAge(24);
        System.out.println("两个对象的内存地址比较:"+(e1==e2));
        System.out.println(e1);
        System.out.println(e2);
    }
    
    
}

Beautifulsen
1 声望1 粉丝

« 上一篇
Servlet相关