Date、BigDecimal、随机数
Date
| 字母 | 日期或时间元素 | 表示 | 示例 | 
|---|---|---|---|
| G | Era | 标志符 | Text | 
| y | 年 | Year | 1996; 96 | 
| M | 年中的月份 | Month | July; Jul; 07 | 
| w | 年中的周数 | Number | 27 | 
| W | 月份中的周数 | Number | 2 | 
| D | 年中的天数 | Number | 189 | 
| d | 月份中的天数 | Number | 10 | 
| F | 月份中的星期 | Number | 2 | 
| E | 星期中的天数 | Text | Tuesday; Tue | 
| a | Am/pm 标记 | Text | PM | 
| H | 一天中的小时数(0-23) | Number | 0 | 
| k | 一天中的小时数(1-24) | Number | 24 | 
| K | am/pm 中的小时数(0-11) | Number | 0 | 
| h | am/pm 中的小时数(1-12) | Number | 12 | 
| m | 小时中的分钟数 | Number | 30 | 
| s | 分钟中的秒数 | Number | 55 | 
| S | 毫秒数 | Number | 978 | 
| z | 时区 | General time zone | Pacific Standard Time; PST; GMT-08:00 | 
| Z | 时区 | RFC 822 time zone | -0800 | 
自定义日期格式
/*
    java中对日期的处理
 */
public class DateTest {
    public static void main(String[] args) {
        //获取系统当前时间(精确到毫秒)
        Date date = new Date();
        System.out.println(date);   //Sat Feb 19 13:45:05 CST 2022
        /*
            yyyy 年
            MM 月
            dd 日
            HH 时
            mm 分
            ss 秒
            SSS 毫秒
         */
        //自定义日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        //SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd HH:mm:ss");
        String format = sdf.format(date);
        System.out.println(format); //2022-02-19 14:00:52 115
        //String --> Date
        String time = "2022-02-02 02:02:02 222";
        //日期字符串的格式要和SimpleDateFormat对象指定的日期格式一致,否则会出现java.text.ParseException异常
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        try {
            Date parse = sdf1.parse(time);
            System.out.println(parse);  //Wed Feb 02 02:02:02 CST 2022
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
currentTimeMillis() 方法
作用:获取自1970年1月1日 00:00:00 000到当前系统时间的总毫秒数
/*
    总结System类的相关属性和方法:
        System.out  out是System类的静态变量
        System.out.println()   println()方法不是System类的,PrintStream类的方法
        System.gc() 建议启动垃圾回收器
        System.currentTimeMillis() 获取自1970年1月1日到系统当前时间的总毫秒数
        System.exit(0) 退出JVM
 */
public class DateTest1 {
    public static void main(String[] args) {
        // 获取自1970年1月1日 00:00:00 000到当前系统时间的总毫秒数
        // 1秒 = 1000毫秒
        long timeMillis = System.currentTimeMillis();
        System.out.println(timeMillis); //1645253925560
        long year = 1970+timeMillis/1000/60/60/24/365;
        System.out.println(year);   //2022
        // 统计一个方法耗时
        // 在调用目标方法之前记录一个毫秒数
        long begin = System.currentTimeMillis();
        print();
        long end = System.currentTimeMillis();
        System.out.println("耗费时长:" + (end - begin) + "毫秒");
    }
    private static void print() {
        for (int i = 0; i < 100; i++) {
            System.out.println("i = " + i);
        }
    }
}
new Date(1) 的含义
含义:1970-01-01 08:00:00 001
public class DateTest2 {
    public static void main(String[] args) {
        //1970-01-01 08:00:00 001
        Date time = new Date(1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String strTime = sdf.format(time);
        System.out.println(strTime);
        //获取昨天此刻的时间
        Date time2 = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
        String strTime2 = sdf.format(time2);
        System.out.println(strTime2);
    }
}
(了解)自定义数字格式
public class DecimalFormatTest {
    public static void main(String[] args) {
        /*
            数字格式:
                # 代表任意数字
                , 代表千分位
                . 代表小数点
                0 代表不够时补0
         */
        //自定义数字格式
        DecimalFormat decimalFormat = new DecimalFormat("###,###.##");
        String s = decimalFormat.format(1234.5);
        System.out.println(s);  //1,234.5
        DecimalFormat decimalFormat1 = new DecimalFormat("###,###.00");
        String s1 = decimalFormat1.format(1234.5);
        System.out.println(s1);  //1,234.50
    }
}
BigDecimal
/*
BigDecimal 属于大数据,精度极高,专门用在财务软件中。
 */
public class BigDecimalTest {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal(100);
        BigDecimal bd2 = new BigDecimal(200);
        //加法
        BigDecimal add = bd1.add(bd2);
        System.out.println(add);    //300
        //减法
        BigDecimal subtract = bd1.subtract(bd2);
        System.out.println(subtract);   //-100
        //乘法
        BigDecimal multiply = bd1.multiply(bd2);
        System.out.println(multiply);   //20000
        //除法
        BigDecimal divide = bd1.divide(bd2);
        System.out.println(divide); //0.5
    }
}
随机数
public class RandomTest {
    public static void main(String[] args) {
        //生成一个随机数
        Random random = new Random();
        int i = random.nextInt();
        System.out.println(i);
        //生成一个[0,100]的随机数
        int i1 = random.nextInt(101);
        System.out.println(i1);
    }
}
随机数练习题
/*
	编写程序,生成5个不重复的随机数,重复的话重新生成。
	最终生成的5个随机数放到数组中
*/
public class RandomTest {
    public static void main(String[] args) {
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++){
            arr[i] = -1;
        }
        //生成随机数
        Random random = new Random();
        //下标
        int index = 0;
        while (index < arr.length){
            //随机产生一个int类型取值范围内的数字
            int num = random.nextInt(5);
            //如果没有重复则输出
            if(!contains(arr,num)){
                arr[index] = num;
                System.out.println("arr[" + index + "]" +"="+ num);
                index++;
            }
        }
    }
    /**
     * @param arr 数组
     * @param key 元素
     * @return
     */
    public static boolean contains(int[] arr, int key){
        for (int i = 0; i < arr.length; i++){
            if(arr[i] == key){
                return true;
            }
        }
        return false;
    }
}










