java学习之路 之 Java常用类-Data类、Math类、BigInteger类、BigDecimai类 及 练习题
来源:互联网 发布:zuk z2 网络较慢 编辑:程序博客网 时间:2024/06/06 07:50
日期类:
java.lang.System类
System类提供的public static longcurrentTimeMillis()用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。此方法适于计算时间差。
计算世界时间的主要标准有:
UTC(Universal Time Coordinated)
GMT(Greenwich Mean Time)
CST(Central Standard Time)
java.util.Date类
表示特定的瞬间,精确到毫秒构造方法:
Date( )使用Date类的无参数构造方法创建的对象可以获取本地当前时间。
Date(long date)
常用方法
getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
toString():把此 Date 对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat),zzz是时间标准。
Date类的API不易于国际化,大部分被废弃了,
java.text.SimpleDateFormat类
是一个不与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 文本)、解析(文本 日期)
格式化:
SimpleDateFormat() :默认的模式和语言环境创建对象
public SimpleDateFormat(String pattern):该构造方法可以用参数pattern指定的格式创建一个对象,该对象调用:
public String format(Date date):方法格式化时间对象date
解析:
public Date parse(String source):从给定字符串的开始解析文本,以生成一个日期。
举例:
Date date = new Date(); //产生一个Date实例//产生一个formater格式化的实例SimpleDateFormat formater = new SimpleDateFormat();System.out.println(formater.format(date));//打印输出默认的格式SimpleDateFormat formater2 = new SimpleDateFormat("yyyy年MM月dd日 EEE HH:mm:ss");System.out.println(formater2.format(date)); //实例化一个指定的格式对象//按指定的格式输出try {Date date2 = formater2.parse(“2008年08月08日 星期一08:08:08"); //将指定的日期解析后格式化按指定的格式输出System.out.println(date2.toString());} catch (ParseException e) {e.printStackTrace();}
java.util.Calendar(日历)类
Calendar是一个抽象基类,主用用于完成日期字段之间相互操作的功能。获取Calendar实例的方法
使用Calendar.getInstance()方法
调用它的子类GregorianCalendar的构造器。
一个Calendar的实例是系统时间的抽象表示,通过get(int field)方法来取得想要的时间信息。比如YEAR、MONTH、DAY_OF_WEEK、HOUR_OF_DAY 、MINUTE、SECOND
public void set(int field,int value)public void add(int field,int amount)public final Date getTime()public final void setTime(Date date)
Math类
java.lang.Math提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般为double型。
abs 绝对值
acos,asin,atan,cos,sin,tan 三角函数
sqrt 平方根
pow(double a,doble b) a的b次幂
log 自然对数
exp e为底指数
max(double a,double b)
min(double a,double b)
random() 返回0.0到1.0的随机数
long round(double a) double型数据a转换为long型(四舍五入)
toDegrees(double angrad) 弧度—>角度
toRadians(double angdeg) 角度—>弧度
BigInteger类
Integer类作为int的包装类,能存储的最大整型值为2的31次方减1,即20多亿。Bigintteger类的数字范围较Integer类的数字范围要大的多。可以支持任意精度的整数。
构造:
Biginteger(String val)
常用方法:
public Biginteger abs()//返回其值是此 BigInteger 的绝对值.public Biginteger add() //返回其值为 (this + val) 的 BigInteger。public BigInteger subtract(BigInteger val) //返回其值为 (this - val) 的 BigInteger。 public BigInteger multiply(BigInteger val) //返回其值为 (this * val) 的 BigInteger。 public BigInteger divide(BigInteger val) //返回其值为 (this / val) 的 BigInteger。 public BigInteger remainder(BigInteger val) //返回其值为 (this % val) 的 BigInteger。 public BigInteger pow(int exponent) //返回其值为 (thisexponent) 的 BigInteger。 public BigInteger[] divideAndRemainder(BigInteger val)//返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
BigDecimal类:
一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类。BigDecimal类支持任何精度的定点数。构造器
public BigDecimal(double val)
public BigDecimal(String val)
常用方法
public BigDecimal add(BigDecimal augend)//返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。public BigDecimal subtract(BigDecimal subtrahend)//返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。public BigDecimal multiply(BigDecimal multiplicand)//返回一个 BigDecimal,其值为 (this × multiplicand),其标度为 (this.scale() + multiplicand.scale())。public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) //返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。
public void TestBigInteger(){BigInteger bi = new BigInteger("12433241123");BigDecimal bd = new BigDecimal("12435.351");BigDecimal bd2 = new BigDecimal("11");System.out.println(bi);//System.out.println(bd.divide(bd2));System.out.println(bd.divide(bd2,BigDecimal.ROUND_HALF_UP));System.out.println(bd.divide(bd2,15,BigDecimal.ROUND_HALF_UP));}
练习题:
import java.math.BigDecimal;import java.math.BigInteger;import java.text.SimpleDateFormat;import java.util.Calendar;import org.junit.Test;public class CommonTest {@Testpublic void test1() {Calendar calendar = Calendar.getInstance();// 1979-8-10calendar.set(Calendar.YEAR, 1979);calendar.set(Calendar.MONTH, 7);calendar.set(Calendar.DAY_OF_MONTH, 10);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(calendar.getTime()));calendar.add(Calendar.DAY_OF_MONTH, -100); // 获取当前对象的日期的100天前的日期System.out.println(sdf.format(calendar.getTime()));}@Testpublic void test2() {System.out.println((int)(Math.random() * 80 + 10));System.out.println(Math.round(3.5)); // 4System.out.println(Math.round(3.4)); // 3System.out.println(Math.round(-3.4)); // -3System.out.println(Math.round(-3.5)); // 也是-3System.out.println(Math.round(-3.6)); // -4}@Testpublic void test3() {BigInteger bi = new BigInteger("2342342342392992929922929292920340489498594954954959459202394234");BigInteger rt = bi.add(new BigInteger("2342342342"));rt = rt.multiply(new BigInteger("100"));System.out.println(rt);BigDecimal bd = new BigDecimal("23423423429883284283482934234.2384293482394293482983423984");bd = bd.pow(20);System.out.println(bd);// 求一个数的阶乘System.out.println(jie(10000));}public String jie(int value) {BigInteger result = new BigInteger("1");for (int i = value; i > 0; i--) {result = result.multiply(new BigInteger(String.valueOf(i)));}return result.toString();}}
3 0
- java学习之路 之 Java常用类-Data类、Math类、BigInteger类、BigDecimai类 及 练习题
- Java学习笔记之常用方法类(三) Data和Calendar类、Math、BigInteger和Random类、Random类
- 类 java.math.BigInteger
- 类 java.math.BigInteger
- 类 java.math.BigInteger
- java.math.BigInteger类
- Java常用类-Math、BigInteger、BigDecimal
- 大数类java.math.BigInteger
- Java.math.BigInteger类实例
- java学习之路 之 Java常用类-StringBuffer类、StringBuilder类 及 练习题
- java学习之路 之 Java常用类-包装类(Wrapper)及练习题
- java学习之路 之 Java常用类-字符串(String)类及练习题
- java学习之BigInteger类、BigDecimal类
- JAVA常用类之Math
- Java常用类库--Math、Random、NumberFormat、DecimalFormat、BigInteger、BigDecimal
- Java的Math-BigInteger-Random类概述
- java学习之Math类
- Java学习之Math类
- 栈和队列面试题(二)
- jsp基础之--使用Session完成简单的登陆操作
- Strusts2原理解析
- 粉书练习之表达式树的实现
- 会议总结20160819
- java学习之路 之 Java常用类-Data类、Math类、BigInteger类、BigDecimai类 及 练习题
- pygame.key 键值说明
- 【Spark Java API】Transformation(6)—aggregate、aggregateByKey
- 记录cocoaPods 安装的详细流程,以及遇到的问题。
- 软件测试分类
- HDU 2589 Phalanx(dp找最大对称矩形)细节及算法详解
- 【基础】常用的机器学习&数据挖掘知识点
- C++中的三种继承public,protected,private
- poj 1155