数学相关的类
Math类
Math类是final类 构造方法私有,不可以创建对象 主要的用法是Math类提供了大量的静态方法 在计算小数的时候不够精确
public class TestMathClass {
@Test
public void testMethod1 ( ) {
System. out. println ( Math. floor ( 1234.567 ) ) ;
System. out. println ( Math. ceil ( 1234.567 ) ) ;
System. out. println ( Math. max ( 123456 , 123 ) ) ;
System. out. println ( Math. min ( 111.11 , 222.11 ) ) ;
System. out. println ( Math. random ( ) * 100 ) ;
}
}
BigDecimal类
用于精确计算的类 在精确计算的时候,要求参数以字符串的方式传入此类的对象
public class TestBigDecimalClass {
@Test
public void testMethod1 ( ) {
BigDecimal bigDecimal1= new BigDecimal ( "123.456" ) ;
BigDecimal bigDecimal2= new BigDecimal ( "123.456" ) ;
BigDecimal bigDecimal3= bigDecimal1. multiply ( bigDecimal2) ;
System. out. println ( bigDecimal3) ;
}
}
BigInteger类
专门处理大整形 用于存储任意大小的整数的类 在存储数据的时候,最好用字符串的方式传入对象
public class TestBigIntegerClass {
@Test
public void testMethod1 ( ) {
int i= 1234567890 ;
BigInteger bi1= new BigInteger ( "1234567890" ) ;
System. out. println ( bi1. intValue ( ) ) ;
BigInteger bi2= new BigInteger ( "123456789123456" ) ;
System. out. println ( bi2. longValue ( ) ) ;
}
}
日期相关的类
Date类
表示日期的类 可以提供很多操作日期的方法,但很多的方法被java标记为过时(Deprecated)
public class TestDateClass {
@Test
public void testMethod1 ( ) {
Date date1= new Date ( ) ;
Date date2= new Date ( 15463215 ) ;
System. out. println ( date1. getYear ( ) ) ;
System. out. println ( date2. getYear ( ) ) ;
}
}
SimpleDateFormat类
parse方法,将日期的字符串转换成日期 Format方法,将日期对象转换成日期的字符串
public class TestSimpleDateFormatClass {
@Test
public void testMethod1 ( ) {
SimpleDateFormat sdf= new SimpleDateFormat ( ) ;
String str = sdf. format ( new Date ( ) ) ;
System. out. println ( str) ;
}
@Test
public void testMethod2 ( ) throws Exception{
SimpleDateFormat sdf= new SimpleDateFormat ( ) ;
Date date = sdf. parse ( "20-1-10 上午10:10" ) ;
System. out. println ( date) ;
}
@Test
public void testMethod3 ( ) {
SimpleDateFormat sdf1= new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss:SSS" ) ;
SimpleDateFormat sdf2= new SimpleDateFormat ( "yyyy年MM月dd日 HH小时mm分钟ss秒SSS毫秒" ) ;
String str1= sdf1. format ( new Date ( ) ) ;
System. out. println ( str1) ;
String str2= sdf2. format ( new Date ( ) ) ;
System. out. println ( str2) ;
SimpleDateFormat sdf3= new SimpleDateFormat ( "yyyy/MM/dd HH/mm/ss/SSS" ) ;
String str3= sdf3. format ( new Date ( ) ) ;
System. out. println ( str3) ;
}
@Test
public void testMethod4 ( ) throws Exception{
SimpleDateFormat sdf1= new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss:SSS" ) ;
SimpleDateFormat sdf2= new SimpleDateFormat ( "yyyy年MM月dd日 HH小时mm分钟ss秒SSS毫秒" ) ;
Date date1= sdf1. parse ( "2019-07-18 10:38:10:123" ) ;
System. out. println ( date1) ;
Date date2= sdf2. parse ( "2019-07-18 10:38:10:123" ) ;
System. out. println ( date2) ;
SimpleDateFormat sdf3= new SimpleDateFormat ( "yyyy/MM/dd HH/mm/ss/SSS" ) ;
Date date3= sdf3. parse ( "2019-07-18 10:38:10:123" ) ;
System. out. println ( date3) ;
}
Calendar类
日历相关的类,控制时区 提供大量的方法来操作时间 Calendar类是一个抽象类,不能直接实例化对象,用Calendar cal=Calendar.getInstance();获取对象
public class TestCalendarClass {
@Test
public void testMethod1 ( ) {
Date date1= new Date ( ) ;
Date date2= new Date ( 15463215 ) ;
Calendar cal1= Calendar. getInstance ( ) ;
System. out. println ( cal1) ;
System. out. println ( cal1. getTimeInMillis ( ) ) ;
System. out. println ( cal1. get ( Calendar. YEAR ) ) ;
System. out. println ( cal1. get ( Calendar. MONTH ) ) ;
System. out. println ( cal1. get ( Calendar. DATE ) ) ;
System. out. println ( cal1. get ( Calendar. HOUR ) ) ;
System. out. println ( cal1. get ( Calendar. MINUTE ) ) ;
cal1. setTime ( date2) ;
System. out. println ( cal1. getTimeInMillis ( ) ) ;
System. out. println ( cal1. get ( Calendar. YEAR ) ) ;
System. out. println ( cal1. get ( Calendar. MONTH ) ) ;
System. out. println ( cal1. get ( Calendar. DATE ) ) ;
System. out. println ( cal1. get ( Calendar. HOUR ) ) ;
System. out. println ( cal1. get ( Calendar. MINUTE ) ) ;
cal1. set ( Calendar. YEAR , 2020 ) ;
}
}
总结
用Date对象存储日期的数据,用Calendar类的对象,操作对象中的日期数据 用SimpleDateFormat做Date对象和日期字符串相互转换