




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第6章时间、日期和数字6.1
Date类6.2
CalendarCalendar类6.3
Math类6.4
BigInteger类
6.1Date类
Date类在java.util包中。使用Date类的无参数构造方法创建的对象可以获取本地当前时间。Date对象表示时间的默认顺序是:星期、月、日、小时、分、秒、年。例如:SatApr2821:59:38CST2001。可以使用DataFormat的子类SimpleDateFormat来实现时期的格式化。SimpleDateFormat有一个常用构造方法:
publicSimpleDateFormat(Stringpattern)。该构造方法可以用参数pattern指定的格式创建一个对象.计算机时间的“公元”设置在1970年1月1日0时(格林威治时间),据此可以使用Date带参数的构造方法:Date(longtime)
例:Datedate1=newDate(1000);Datedate2=newDate(-1000);此时,date1的时间就是1970年01月01日08时00分01秒,date2的时间就是1970年01月01日07时59分59秒。System类的静态方法publiclongcurrentTimeMillis()也可获得系统的当前时间。这是从1970年01月01日00时00分00秒到目前时刻所走过的毫秒数。例:importjava.util.*;importjava.text.SimpleDateFormat;publicclassTestDate{ publicstaticvoidmain(Stringargs[]){ Datenowtime=newDate(); System.out.println(nowtime); SimpleDateFormatmatter1= newSimpleDateFormat("'time':yyyy年MM月dd日E北京时间"); System.out.println(matter1.format(nowtime)); SimpleDateFormatmatter2= newSimpleDateFormat("北京时间:yyyy年MM月dd日HH时mm分ss秒"); System.out.println(matter2.format(nowtime)); Datedate1=newDate(1000),date2=newDate(-1000); System.out.println(matter2.format(date1)); System.out.println(matter2.format(date2)); System.out.println(newDate(System.currentTimeMillis())); }}屏幕上输出的结果是:?6.2Calendar类
Calendar类在java.util包中。使用Calendar类的static方法getInstance()可以初始化一个日历对象,如:Calendarcalendar=calendar.getInstance();然后,calendar对象可以调用方法:set(intyear,intmonth,intdate)set(intyear,intmonth,intdate,inthour,intminute)set(intyear,intmonth,intdate,inthour,intminute,intsecond)将日历翻到任何一个时间,当参数year取负数时表示公元前。calendar对象调用方法:
publicintget(intfield)可以获取有关年份、月份、小时、星期等信息,参数field的有效值由Calendar的静态常量指定,例如:calendar.get(Calendar.MONTH);返回一个整数,如果该整数是0表示当前日历是在一月,该整数是1表示当前日历是在二月等。日历对象调用publiclonggetTimeInMillis()可以将时间表示为毫秒。例:importjava.util.*;importjava.text.SimpleDateFormat;classTestCalendar{ publicstaticvoidmain(Stringargs[]){ Calendarcal=Calendar.getInstance(); cal.setTime(newDate()); Stringyear=String.valueOf(cal.get(Calendar.YEAR)), month=String.valueOf(cal.get(Calendar.MONTH)+1), day=String.valueOf(cal.get(Calendar.DAY_OF_MONTH)), week=String.valueOf(cal.get(Calendar.DAY_OF_WEEK)-1); inthour=cal.get(Calendar.HOUR_OF_DAY), minute=cal.get(Calendar.MINUTE), second=cal.get(Calendar.SECOND); System.out.println("现在的时间是:"); System.out.println(""+year+"年"+month+"月"+day+"日"+"星期"+week); System.out.println(""+hour+"时"+minute+"分"+second+"秒"); cal.set(1985,5,29);//将日历翻到1985年6月29日 longtime1985=cal.getTimeInMillis(); cal.set(2009,9,29); longtime2009=cal.getTimeInMillis(); longday_num=(time2009-time1985)/(1000*60*60*24); System.out.println("2006年10月29日和1962年6月29日相隔"+day_num+"天"); }}屏幕上输出的结果是:?再例:importjava.util.*;classCalendarPage{ publicstaticvoidmain(Stringargs[]){ System.out.println("日一二三四五六"); Calendarcal=Calendar.getInstance(); cal.set(2009,9,1);//将日历放到09年10月1日 intweek=cal.get(Calendar.DAY_OF_WEEK)-1; Stringa[]=newString[week+31];//存放号码的一维数组 for(inti=0;i<week;i++) a[i]="**"; for(inti=week,n=1;i<week+31;i++){ if(n<=9) a[i]=String.valueOf(n)+""; else a[i]=String.valueOf(n); n++; } for(inti=0;i<a.length;i++){ if(i%7==0) System.out.println(""); System.out.print(""+a[i]); } }}输出上述日历页如何编程实现?6.3Math类
在编写程序时,可能需要计算一个数的平方根、绝对值、获取一个随机数等等。java.lang包中的类包含许多用来进行科学计算的类方法,这些方法可以直接通过类名调用。另外,Math类还有两个静态常量,E和PI,它们的值分别是:
2.7182828284590452354和
3.14159265358979323846。Math类常用方法
publicstaticdoubleabs(doublea)返回a的绝对值。publicstaticdoublemax(doublea,doubleb)返回a、b的最大值。publicstaticdoublemin(doublea,doubleb)返回a、b的最小值。publicstaticdoublerandom()产生一个0到1之间的随机数(不包括0和1)。publicstaticdoublepow(doublea,doubleb)返回a的b次幂。publicstaticdoublesqrt(doublea)返回a的平方根。publicstaticdoublelog(doublea)返回a的对数。publicstaticdoublesin(doublea)返回正弦值。publicstaticdoubleasin(doublea)返回反正弦值。
对数字结果格式化
怎样将3.1415926输出显示为:003.1416,这需要用到java.text包中的NumberFormat类。
实例化NumberFormat对象调用类方法:publicstaticfinalNumberFormatgetInstance()格式化数字调用方法:publicfinalStringformat(doublenumer)NumberFormat类有如下常用方法:publicvoidsetMaximumFractionDigits(intnewValue)publicvoidsetMinimumFractionDigits(intnewValue)publicvoidsetMaximumIntegerDigits(intnewValue)publicvoidsetMinimumIntegerDigits(intnewValue)6.4BigInteger类
程序有时需要处理大整数,java.math包中的BigInteger类提供任意精度的整数运算。可以使用构造方法:
publicBigInteger(Stringval)构造一个十进制的BigInteger对象。注意:当字符串val中含有非数字字符时,则发生NumberFormatException异常BigInteger类的常用方法
publicBigIntegeradd(BigIntegerval)返回当前大整数对象与参数指定的大整数对象的和。publicBigIntegersubtract(BigIntegerval)返回当前大整数对象与参数指定的大整数对象的差。publicBigIntegermultiply(BigIntegerval)返回当前大整数对象与参数指定的大整数对象的积。publicBigIntegerdivide(BigIntegerval)返回当前大整数对象与参数指定的大整数对象的商。publicBigIntegerremainder(BigIntegerval)返回当前大整数对象与参数指定的大整数对象的余。BigInteger类的常用方法(续)
publicintcompareTo(BigIntegerval)返回当前大整数对象与参数指定的大整数对象的比较结果:1、-1或0。publicBigIntegerabs(BigIntegerval)返回当前大整数对象的绝对值。publicBigIntegerpow(intexponent)返回当前大整数对象的exponent次幂。publicStringtoString()返回当前大整数对象十进制的字符串表示。publicStringtoString(intp)返回当前大整数对象p进制的字符串表示。例:计算1+2!+3!+前30项之和。例:importjava.math.*;publicclassTestBigInteger{ publicstaticvoidmain(Stringargs[]){ BigIntegersum=newB
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- Unit 2 Sports and Fitness Lesson 2 Rules of the Game 第一课时教学设计-2024-2025学年高中英语北师大版(2019)必修第一册
- 借款合同范本2016
- 貂皮抵押合同范本
- 机械代工合同范本
- 习作:故事新编 教学设计-2023-2024学年四年级下册语文统编版
- 2025至2030年双电机卷取式天棚帘项目投资价值分析报告
- 2024-2030年中国军用服装行业发展监测及投资前景预测报告
- 2025至2030年免漆环保防水板项目投资价值分析报告
- 2025年萘系高浓高效减水剂项目可行性研究报告
- 2025至2030年中国德式镀锌喉箍数据监测研究报告
- 六年级下册语文第一单元测试卷 部编版(含答案)
- 2024年湖南高速铁路职业技术学院单招职业适应性测试题库新版
- 《研学旅行市场营销》课件-研学旅行市场营销之社群营销
- clsim100-32药敏试验标准2023中文版
- LNG加气站质量管理手册
- 艰难梭菌感染动物模型的建立及其应用评价
- (正式版)HGT 22820-2024 化工安全仪表系统工程设计规范
- 2024年公安部直属事业单位招聘笔试参考题库附带答案详解
- 《旅游景点云南》课件2
- 《肺癌课件:基本概念与临床表现》
- 《斯特鲁普效应》课件
评论
0/150
提交评论