您的当前位置:首页正文

Java内部类五:其他类

2024-11-28 来源:个人技术集锦

BigDecimal

代码演示

import java.math.BigDecimal;
public class Test02 {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal("1.0");
        BigDecimal bd2 = new BigDecimal("0.3");
        //add()加
        BigDecimal r1 = bd1.add(bd2);
        //subtract()减
        BigDecimal r2 = bd1.subtract(bd2);
        //multiply()乘
        BigDecimal r3 = bd1.multiply(bd2);
        //divide()除,2表示保留两位小数,ROUND_HALF_UP四舍五入
        BigDecimal r4 = bd1.divide(bd2,2,BigDecimal.ROUND_HALF_UP);
        System.out.println(r1);
        System.out.println(r2);
        System.out.println(r3);
        System.out.println(r4);
    }
}

Date类

import java.util.Date;
public class Test03 {
    public static void main(String[] args) {
        //获取当前日期
        Date date1 = new Date();
        System.out.println(date1.toString());
        //获取时间戳,自1970.1.1 0时
        System.out.println(date1.getTime());
        //获取昨天当前日期
        Date date2 = new Date(date1.getTime() - (24*60*60*1000));
        System.out.println(date2);
        //判断日期前后
        boolean b1 = date1.after(date2);
        System.out.println(b1);
        boolean b2 = date1.before(date1);
        System.out.println(b2);
        //比较
        int d = date1.compareTo(date2);
        System.out.println(d);
        //比较是否相等
        boolean b3 = date1.equals(date2);
        System.out.println(b3);
    }
}

Calendar

import java.util.Calendar;
public class Test04 {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        //打印当前时间
        System.out.println(calendar.getTime().toLocaleString());
        //打印时间戳
        System.out.println(calendar.getTimeInMillis());
        //获取年
        int year = calendar.get(Calendar.YEAR);
        //获取月
        int month = calendar.get(Calendar.MONTH);
        //获取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        //获取时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        //获取分
        int minute = calendar.get(Calendar.MINUTE);
        //获取秒
        int second = calendar.get(Calendar.SECOND);
        System.out.println(year + "年" + (month+1) + "月"
        + day + "日" + hour + "时" + minute + "分"
        + second + "秒");
        //修改时间
        calendar.set(Calendar.DAY_OF_MONTH,8);
        System.out.println(calendar.getTime().toLocaleString());
        //add()方法修改时间
        calendar.add(Calendar.YEAR,2);
        System.out.println(calendar.getTime().toLocaleString());
        //获取最大最小
        int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int min = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        System.out.println(max);
        System.out.println(min);
    }
}

SimpleDateFormat

package com.zheng.qq;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test05 {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
        Date date = new Date();
        //把日期转换为字符串
        String str = sdf.format(date);
        System.out.println(str);
        //把字符串转换为日期
        Date date1 = sdf.parse("1999年4月16日20:45:46");
        System.out.println(date1);
    }
}

System类

public class Test07 {
    public static void main(String[] args) {
        //1.复制数组
        //src:原数组,srcPos:从哪个位置开始复制
        //dest:目标数组,destPos:目标数组位置
        //length:复制的长度
        int[] num = {1,2,3,4};
        int[] num1 = new int[4];
        System.arraycopy(num,1,num1,0,3);
        for (int i = 0; i < num1.length; i++) {
            System.out.println(num1[i]);
        }
        //打印时间戳
        System.out.println(System.currentTimeMillis());
    }
}

总结

显示全文