BigInteger、BigDecimal、Date、SimpleDateFormat、Calendar
BigInteger 类
可以让超过 Integer 范围内的数据进行运算
构造方法
public BigInteger(String val)
成员方法
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)
:返回包含(this / val)
后跟(this % val)
两个BigInteger
的(长度为 2)数组
BigDecimal 类
由于在运算的时候,float类型和double
很容易丢失精度
,所以,为了能精确的表示、计算浮点数,Java 提供了BigDecimal不可变的、任意精度
的有符号十进制数。
构造方法
public BigDecimal(String string)
成员方法
public BigDecimal add(BigDecimal augend)
:加法
public BigDecimal subtract(BigDecimal subtrahend)
:减法
public BigDecimal multiply(BigDecimal multiplicand)
:乘法
public BigDecimal divide(BigDecimal divisor)
:除法(当除不尽时,此方法会抛出异常)
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
:除法。指定精度,指定舍入模式
Date 类
类 Date 表示特定的瞬间,精确到毫秒
构造方法
public Date()
:使用当前系统时间构造一个Date
。
public Date(long date)
:使用一个毫秒值构造一个Date
。
成员方法
public long getTime()
:获取毫秒值。
public void setTime(long time)
:设置毫秒值。
SimpleDateFormat 类
DateFormat
是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类
构造方法
public SimpleDateFormat()
:用默认的模式和默认语言环境的日期格式符号构造SimpleDateFormat
。
public SimpleDateFormat(String pattern)
:用给定的模式和默认语言环境的日期格式符号构造SimpleDateFormat
。
成员方法
public final String format(Date date)
:将一个Date
转换为String
。
public Date parse(String source)
:将一个String
转换为Date
。
注意:当前SimpleDateFormat
的对象的格式一定要与参数source
表示的格式一致,否则会抛出异常。
/**
* Date 操作
* @author whoamizq
*/
public class DateTest {
public static void main(String[] args) {
long time = System.currentTimeMillis();
time = 1583978814000l;// 精确至毫秒的时间戳
Date date = new Date(time);
System.out.println(date.toString());
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
// 调用 format 方法对日期对象进行格式化。 用默认风格。
String str_date = dateFormat.format(date);
System.out.println(str_date);
}
}
Calendar 类
Calendar
类是一个抽象类,它为特定瞬间与一组诸如YEAR、MONTH、DAYOFMONTH、HOUR
等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
成员方法
public static Calendar getInstance()
:使用默认时区和区域设置获取日历。通过该方法生成Calendar
对象。
public int get(int field)
:返回给定日历字段的值。所谓字段就是年、月、日等等。
成员方法
public void add(int field,int amount)
:按照日历的规则,给指定字段添加或减少时间量。
public final void set(int year,int month,int date)
:设置日历的年、月、日等。
希望能帮助到有需要的人。