4.13 Date Calendar Random BigInterger/BigDecimal

Date类

Date date = new Date();
System.out.println(date.getTime());

日期格式化SimpleDateFormat

        
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
        // 反格式化
        String ss = "2022:06:30 13:01:00";
        System.out.println(simpleDateFormat.parse(ss));

日历类Calendar

        // 单例模式
        Calendar calendar = Calendar.getInstance();
        // 获取当前时间
        System.out.println(calendar.getTime());
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
        // 本月的第几周
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));

随机数Random

 // 创建对象
        Random random = new Random();
        // 【0,1)之间的小数
        System.out.println(random.nextDouble());
        // 生成0到99之间的数
        System.out.println(random.nextInt(100));
        // 随机A~Z
        int i = random.nextInt('Z'-'A'+1)+'A';
        System.out.println((char)i);

Math类

System.out.println(Math.abs(-12));
        System.out.println(Math.sqrt(14));
        System.out.println(Math.pow(2, 3));
        System.out.println(Math.ceil(2.1));
        System.out.println(Math.floor(2.6));
        // 四舍五入
        System.out.println(Math.round(3.4));
        System.out.println(Math.round(3.5));
        // 四舍六入五留双
        System.out.println(Math.rint(2.2));
        System.out.println(Math.rint(2.7));
        System.out.println(Math.rint(2.5));
        System.out.println(Math.rint(3.5));

数字格式化DecimalFormat

/**
 * 数字格式化 :
 * # 任意数字 , 0-9任意单个数字
 * , 千分位
 * . 小数点
 * 0 补位
 */
public class Number_01 {
	public static void main(String[] args) {
		// 创建数字格式化对象
		// 需求 : 千分位分割
		DecimalFormat df = new DecimalFormat("###,###");
		System.out.println(df.format(12343356.231));

		// 需求 : 千分位分割,并且保留两位小数
		df = new DecimalFormat("###,###.##");
		System.out.println(df.format(12343356.236));
		// 需求 : 千分位分割,并且保留4位小数,不够补0
		df = new DecimalFormat("###,###.####");
		System.out.println(df.format(12343356.236));
		// 需求 : 千分位分割,并且保留4位小数
		df = new DecimalFormat("###,###.0000");
		System.out.println(df.format(12343356.236));
	}
}

Number类(精度更高的数字类型)

// 精度更高的数据类型
		// 整数更高精度
		BigInteger bi = new BigInteger("22");
		// 小数更高精度
		BigDecimal bd = new BigDecimal(44.3);
		// 进行运算时  不能使用 + - *  /  % ,已经封装成方法
		BigDecimal b1 = new BigDecimal(15);
		BigDecimal b2 = new BigDecimal(5);
		// +
		BigDecimal v1 = b1.add(b2);
		// - 
		BigDecimal v2 = b1.subtract(b2);
		// * 
		BigDecimal v3 = b1.multiply(b2);
		// /
		BigDecimal v4 = b1.divide(b2);
		// %
		BigDecimal v5 = b1.remainder(b2);

  • 14
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值