日期类:
java.lang.System类
System类提供的public static long currentTimeMillis()用来返回当前时间与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 {
@Test
public void test1() {
Calendar calendar = Calendar.getInstance();
// 1979-8-10
calendar.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()));
}
@Test
public void test2() {
System.out.println((int)(Math.random() * 80 + 10));
System.out.println(Math.round(3.5)); // 4
System.out.println(Math.round(3.4)); // 3
System.out.println(Math.round(-3.4)); // -3
System.out.println(Math.round(-3.5)); // 也是-3
System.out.println(Math.round(-3.6)); // -4
}
@Test
public 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();
}
}