编程语言
首页 > 编程语言> > java其他类

java其他类

作者:互联网

1.BigDecimal类

public class BigDecimalDemo {
    public static void main(String[] args) {
        double d1=1.0;
        double d2=0.9;
        System.out.println(d1-d2);//0.09999999999999998
        System.out.println((1.4-0.5)/0.9);//0.9999999999999999
    }
}
public class BigDecimalDemo {
    public static void main(String[] args) {
        BigDecimal b1 = new BigDecimal("1.0");
        BigDecimal b2 = new BigDecimal("0.9");
        //加
        BigDecimal add = b1.add(b2);
        System.out.println(add);//1.9
        //减
        System.out.println(b1.subtract(b2));//0.1
        //乘
        System.out.println(b1.multiply(b2));//0.90
        //除,若不进行限制,可能会报错,BigDecimal.ROUND_HALF_UP为四舍五入,scale为保留几位小数
        System.out.println(b1.divide(b2,2,BigDecimal.ROUND_HALF_UP));//1.11
        BigDecimal divide = new BigDecimal("1.4")
                .subtract(new BigDecimal("0.5"))
                .divide(new BigDecimal("0.9"),2,BigDecimal.ROUND_HALF_UP);
        System.out.println(divide);//1.00
    }
}

2.Date类

import java.util.Date;
public class DateDemo {
    public static void main(String[] args) {
        Date date1=new Date();
        System.out.println(date1.toString());
        System.out.println(date1.toLocaleString());
        //当前时间减1天的毫秒数
        Date date2=new Date(date1.getTime()-(60*60*24*1000));
        System.out.println(date2.toString());//Sat Jan 08 22:22:37 CST 2022
        System.out.println(date2.toLocaleString());//2022-1-8 22:22:37
        //方法
        //1.after before
        System.out.println(date1.after(date2));//true
        System.out.println(date1.before(date2));//false
        //2.compareTo equals
        //compareTo:前者比后者大则返1,小则返回-1,相等返回0
        System.out.println(date1.compareTo(date2));//1
        System.out.println(date2.compareTo(date1));//-1
        System.out.println(date1.compareTo(date1));//0
        System.out.println(date1.equals(date2));//false
    }
}

3.Calendar类

image-20220109223134286

import java.util.Calendar;
public class CalendarDemo {
    public static void main(String[] args) {
        //Calendar是抽象类,无法直接实例化
        Calendar calendar=Calendar.getInstance();
        System.out.println(calendar.getTime().toLocaleString());
        //方法:get
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(1));
        System.out.println(calendar.get(Calendar.MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));//Carlendar.HOUR为12进制表示小时
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));
        //set
        calendar.set(Calendar.HOUR_OF_DAY,13);
        System.out.println(calendar.getTime().toLocaleString());
        //add
        calendar.add(Calendar.MINUTE,-10);
        System.out.println(calendar.getTime().toLocaleString());
        //getTimeInMillis获得一个毫秒数
        long timeInMillis = calendar.getTimeInMillis();//获得一个毫秒数
        System.out.println(timeInMillis);
        //getActualMaximum/getActualMaximum
        System.out.println(calendar.getActualMaximum(Calendar.MONTH));//11月份是0开始的
        System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    }
}

4.SimpleDateFormat类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo {
    public static void main(String[] args) throws ParseException {
        //方法一:第一种构造方法,不带参数
        SimpleDateFormat simpleDateFormat1=new SimpleDateFormat();
        simpleDateFormat1.applyPattern("yyyy-MM-dd");
        //格式化format(Date date):将时间格式转换成String
        System.out.println(simpleDateFormat1.format(new Date()));
        //第二种构造方法
        SimpleDateFormat simpleDateFormat2=new SimpleDateFormat("y-M-d H:m:s");
        System.out.println(simpleDateFormat2.format(new Date()));//2022-1-10 9:23:58
        //解析:将String转成Date类型parse(String)
        String str="2099-19-10";
        //会抛出一个异常
        Date date = simpleDateFormat1.parse(str);
        System.out.println(date);//Sat Jul 10 00:00:00 CST 2100
    }
}

5.System类

image-20220110223946627

import java.util.Arrays;
public class SystemDemo {
    public static void main(String[] args) {
        int []arr1={9,1,4556,4456,234,346,123,234};
        int []arr2=new int[8];
        //实现数组的copy
        //参数1:源数组 ;参数2:从源数组的哪个位置开始copy;
        //参数3:目标数组;参数4:从目标数组的哪个位置开始copy;
        //参数5:copy长度
        System.arraycopy(arr1,4,arr2,4,4);
        for (int x:arr2) {
           System.out.println(x);
        }
        //Arrays.copyOf底层是调用的System.arraycopy
        //System.arraycopy方法是本地方法,有native(相当于调用c或c++)修饰,执行效率高
        int[] ints = Arrays.copyOf(arr1, 8);
        //获取当前毫秒数(1970年到现在),此方法一般用于计算一段程序执行的时间
        System.out.println(System.currentTimeMillis());
        long start=System.currentTimeMillis();
        for (int i = 0; i < 999999; i++) {
            for (int j = 0; j < 999999; j++) {
                int result=i+j;
            }
        }
        long stop=System.currentTimeMillis();
        System.out.println(stop-start);
        //gc垃圾回收器,调用此方法请求jvm启动垃圾回收器,但不一定会回收垃圾
        new Student();//Student类在下面
        new Student("abc",1);
        new Student("cba",6);
        System.gc();
        //回收了cba6
        //回收了abc1
        //回收了null0

        //exit方法退出jvm
        System.out.println("我正在退出...");
        System.exit(0);//0表示正常退出
        System.out.println("我已经退出了");//已经退出jvm,所以这句不打印
    }
}
public class Student {
    private String name;
    private int 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 Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public Student() {
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    protected void finalize() throws Throwable {
        System.out.println("回收了"+this.name+this.age);
    }
}

标签:java,BigDecimal,System,其他,println,Calendar,public,out
来源: https://www.cnblogs.com/sp520/p/15794731.html