Java中的SimpleDateFormat,Biginteger,BigDecimal

Java中的SimpleDateFormat 、BigInteger 、BigDecimal

1.SimpleDateFormat

SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化。

SimpleDateFormat 允许以为日期-时间格式化选择任何用户指定的方式启动。 但是,希望用 DateFormat 中的 getTimeInstancegetDateInstancegetDateTimeInstance 创建一个日期-时间格式化程序。 每个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。 可以根据需要用 applyPattern 方法修改格式化方式。

SimpleDateFormat函数的继承关系:
java.lang.Object
|
±—java.text.Format
|
±—java.text.DateFormat
|
±—java.text.SimpleDateFormat

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

public class Simple {
    public static void main(String[] args) {
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
        SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()
        SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
        SimpleDateFormat myFmt4=new SimpleDateFormat(
                "一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
        Date now=new Date();
        System.out.println(myFmt.format(now));
        //2020年11月12日 19时38分10秒当前时间
        System.out.println(myFmt1.format(now));
        //20/11/12 19:38
        System.out.println(myFmt2.format(now));
        //2020-11-12 19:38:10
        System.out.println(myFmt3.format(now));
        //2020年11月12日 19时38分10秒 星期四 
        System.out.println(myFmt4.format(now));
        //一年中的第 317 天 一年中第46个星期 一月中第2个星期 在一天中19时 CST时区
        System.out.println(now.toGMTString());
        //12 Nov 2020 11:38:10 GMT
        System.out.println(now.toLocaleString());
        //2020-11-12 19:38:10
        System.out.println(now.toString());
        //Thu Nov 12 19:38:10 CST 2020
    }

}

2.BigInteger BigDecimal

如果基本的整数和浮点精度不能满足要求,那么可以使用Java.math包中的两个很有用的类:BigInteger 和 BigDecimal。这两个类可以处理包含任意长度数字序列的数值。BigInteger类实现了任意精度的整数运算,BigDecimal类实现了任意精度的浮点数运算。

​ 使用静态的valueof方法可以将普通的数值转换为大数值:

例如:BigInteger a = BigInteger.valueof(1000);

BigInteger类无法像int 定义的类型进行+,*符号运算,但是可以使用BigInteger中的方法来实现:

BigInteger b = a.add©;//b = a+c;

BigInteger b = a.multipiy©;//b = a*c;

public class BigINT{
    public static void main(String[] args) {
        BigInteger bi1 = new BigInteger("123456789") ;
        // 声明BigInteger对象
        BigInteger bi2 = new BigInteger("987654321") ;
        // 声明BigInteger对象
        System.out.println("加法操作:" + bi2.add(bi1)) ;
        // 加法操作
        System.out.println("减法操作:" + bi2.subtract(bi1)) ;
        // 减法操作
        System.out.println("乘法操作:" + bi2.multiply(bi1)) ;
        // 乘法操作
        System.out.println("除法操作:" + bi2.divide(bi1)) ;
        // 除法操作
        System.out.println("最大数:" + bi2.max(bi1)) ;
        // 求出最大数
        System.out.println("最小数:" + bi2.min(bi1)) ;
        // 求出最小数
        BigInteger result[] = bi2.divideAndRemainder(bi1) ;
        // 求出余数的除法操作
        System.out.println("商是:" + result[0] +
                ";余数是:" + result[1]) ;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值