常用类"三"(BigInteger,BigDecimal,Date)
来源:互联网 发布:芒果店长软件下载 编辑:程序博客网 时间:2024/06/05 04:31
BigInteger类同Integer类一样,都是处理整数的包装类,它们的区别在于,Integer处理的整数范围:-2147483648~2147483647,当超出这个范围时,Integer就处理不了,此时就需要使用BigInteger来处理了。
常用构造方法:
BigInteger (byte[] val)BigInteger (String val)
代码实现:
byte[] val={0,0,1,0}; BigInteger bi=new BigInteger(val); System.out.println(bi); BigInteger bi0=new BigInteger("999"); System.out.println(bi0);
常用方法和代码:
/* * public BigInteger add(BigInteger val):加 * public BigInteger subtract(BigInteger val):减 * public BigInteger multiply(BigInteger val):乘 * public BigInteger divide(BigInteger val):除 * public BigInteger[] divideAndRemainder(BigInteger val) * :返回商和余数的数组 */public class BigIntegerDo { public static void main(String[] args) { BigInteger b1=new BigInteger("100"); BigInteger b2=new BigInteger("55");//public BigInteger add(BigInteger val) System.out.println("add="+b1.add(b2));//155//public BigInteger subtract(BigInteger val) System.out.println("substact="+b1.subtract(b2));//45// public BigInteger multiply(BigInteger val) System.out.println("multiply="+b1.multiply(b2));//5500//public BigInteger divide(BigInteger val) System.out.println("divide="+b1.divide(b2));//1//public BigInteger[] divideAndRemainder(BigInteger val) BigInteger[] b3=b1.divideAndRemainder(b2); System.out.println("商="+b3[0]);//1 System.out.println("余数="+b3[1]);//45
BigDecimal:由于在运算时,float类型和double很容易丢失精度。对一些严格的项目来说,比如金融,这种精度丢失是不能容忍的。所以java就提供BigDecimal类,用来操作对精度要求高的数据;
构造方法:
//以BigInteger类为参数 public BigDecimal(BigInteger val);//以double类型为参数 public BigDecimal(double val);//以int类型为参数 public BigDecimal(int val);//以long类型为参数 public BigDecimal(long val);//以字符串为参数 public BigDecimal(String val);
常用方法:
/* * public BigDecimal add(BigDecimal augend); * public BigDecimal subtract(BigDecimal subtractend) * public BigDecimal multiply(BigDecimal multiplycand); * public BigDecimal divide(BigDecimal divisor); * public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode) * divisor:除数 * scale:要保留的位数 * roundingMode:选择舍去的模式,一般选ROUND_HALF_UP */public class BigDecimalDemo { public static void main(String[] args) { BigDecimal b1=new BigDecimal(0.9); BigDecimal b2=new BigDecimal(0.4);// 加法演示: System.out.println(b1.add(b2));//* public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode) System.out.println("divide="+b1.divide(b2,2,BigDecimal.ROUND_HALF_UP)); }}
Date:表示特定的瞬间,精确到毫秒
构造方法:
//根据当前默认时间的毫秒值创建日期对象public Date();//根据给定的毫秒值创建日期对象public Date(long date);
常用方法:
public long getTime();//获取当前之间的毫秒值public void setTime(long time);//根据给定的毫秒值设置时间public String toString();//把Date对象转换成String对象
public static void main(String[] args) { Date d = new Date(); System.out.println(d);// 获取毫秒值 System.out.println(d.getTime()); //根据给定的毫秒值设置时间 long time =2147483649L; d.setTime(time); System.out.println("d="+d); String s=d.toString(); System.out.println(s); }
DateFormat:是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。日期/时间格式化子类(如 SimpleDateFormat)允许进行格式化(也就是日期 -> 文本)、解析(文本-> 日期)和标准化.
DateFormat:可以进行日期和字符串的格式化和解析,但是由于是抽象的,所以使用具体子类SimpleDateFormat
SimpleDateFormat的构造函数:
public SimpleDateFormat():以默认的模式public SimpleDateFormat(String pattern):以给定的模式pattern的对应表:年y,月M,日d,时H,分m,秒s.
1:把时间格式化为时间:
public static void main(String[] args) { //获取当前时间的毫秒值 Date d=new Date(); SimpleDateFormat sdf=new SimpleDateFormat(); //调用SimpleDateFormat的方法或父类方法把时间格式为指定模式的时间 String s=sdf.format(d); System.out.println(d);//Mon MMM 06 13:49:15 CST 2005(默认模式) //指定模式 SimpleDateFormat sdf1=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String s1=sdf1.format(d);//format方法是DateFormat类中格式化时间的 System.out.println(s1); }
2:把日期解析成时间
//假如把2000年11月11日 11时11分11秒解析成时间String s="2000年11月11日 11时11分11秒";//创建SimpleDateFormatSimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// 然后调用给定模式类中的解析方法parse(),把String类型的日期装换成Date类型 Date ds=sdf1.parse(s);//parse()方法是DateFormat中解析时间的方法 System.out.println(ds);
Calendar:
/* * Calendar:此抽象类为特定瞬间与异族如YEAR,MONTH,HOUR等日历字段之间的转换提供 * 方法,并为日历字段提供方法 * public int get(int field):返回给定日历字段的值, * 日历类中的每个日历字段都是静态的成员变量,并且是int类型 */public class CalendarDemo { public static void main(String[] args) { //创建日历对象时,日历字段已由当前日期和时间初始化了。 Calendar now=Calendar.getInstance();//通过静态方法获取子类对象 //获取年 int year=now.get(Calendar.YEAR);//get的参数全部是静态成员变量 //获取月 int month=now.get(Calendar.MONTH); //获取日 int day=now.get(Calendar.DAY_OF_MONTH); System.out.println(year+"年"+(month+1)+"月"+day+"日"); //month是从0开始的,需要+1
0 0
- 常用类"三"(BigInteger,BigDecimal,Date)
- 常用类-BigInteger类、BigDecimal类、Date/DateFormat类、Calendar类
- JavaSE----API之常用类(Math、Random、System、BigInteger和BigDecimal、Date和DateFormat、Calendar)
- 14 API-常用类(正则表达式(Pattern,Matcher),Math,Random,System,BigInteger,BigDecimal,Date/DateFormat,Calendar)
- Java常用类-Math、BigInteger、BigDecimal
- Java中正则表达式和常用的Math、Random、System、BigInteger、BigDecimal、Date/DateFormat、Calendar类的使用
- java BigInteger BigDecimal类
- BigInteger和BigDecimal类
- java/System/BigInteger/BigDecimal/Date/Calender
- Java常用类库--Math、Random、NumberFormat、DecimalFormat、BigInteger、BigDecimal
- JAVA常用类之——BigInteger和BigDecimal
- java常用类库---大数操作(BigInteger,BigDecimal)
- 11.2常用类(Arrays类,Bigdecimal类,BigInteger类,System类,Calendar类,Date类,Math类,Random类,正则表达式)
- BigInteger类和BigDecimal类
- Java-BigDecimal类,BigInteger类
- 高精度运算类(BigInteger,BigDecimal)
- 类:BigDecimal,BigInteger,Math,System
- JAVA基础复习十四-Math、Random、System、BigInteger、BigDecimal、Date、SimpleDateFormat类
- UGUI Make Use Of Grid Layout Group
- 第五周 程序阅读——static(1)
- 派生类的指针绑定到基类的对象(编译通过,但结果不可预知)
- 多线程之生产者-消费者
- oracle登录无响应问题解决方法(oracle4612267补丁安装教程)
- 常用类"三"(BigInteger,BigDecimal,Date)
- 数据集的基本信息(二)
- Visual Studio工程里的自定义宏
- 蓝桥杯-基础练习-数列排序
- viewstatus
- 继承
- 计算机网络之 网络层
- 第五周 程序阅读——static(2)
- 值传递和引用传递-----函数参数传递的两种方式