Java进阶:Date、BigDecimal、随机数

Date

字母日期或时间元素表示示例
GEra标志符Text
yYear1996; 96
M年中的月份MonthJuly; Jul; 07
w年中的周数Number27
W月份中的周数Number2
D年中的天数Number189
d月份中的天数Number10
F月份中的星期Number2
E星期中的天数TextTuesday; Tue
aAm/pm 标记TextPM
H一天中的小时数(0-23)Number0
k一天中的小时数(1-24)Number24
Kam/pm 中的小时数(0-11)Number0
ham/pm 中的小时数(1-12)Number12
m小时中的分钟数Number30
s分钟中的秒数Number55
S毫秒数Number978
z时区General time zonePacific 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值