数学和日期相关的类

it2022-05-05  140

数学和日期相关的类

数学相关的类Math类BigDecimal类BigInteger类 日期相关的类Date类SimpleDateFormat类Calendar类总结

数学相关的类

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); } //下面的两种写法是推荐的写法 //但是要注意几种写法xxxx年xx月xx日 xx:xx:xx:xxx 或 xxxx-xx-xx xx:xx:xx:xxx 或 xxxx/xx/xx xx:xx:xx:xxx @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(); //自定long毫秒的日期 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对象和日期字符串相互转换

最新回复(0)