Java I 6时间日期数字.ppt

上传人:牧羊曲112 文档编号:5435936 上传时间:2023-07-06 格式:PPT 页数:17 大小:312.49KB
返回 下载 相关 举报
Java I 6时间日期数字.ppt_第1页
第1页 / 共17页
Java I 6时间日期数字.ppt_第2页
第2页 / 共17页
Java I 6时间日期数字.ppt_第3页
第3页 / 共17页
Java I 6时间日期数字.ppt_第4页
第4页 / 共17页
Java I 6时间日期数字.ppt_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《Java I 6时间日期数字.ppt》由会员分享,可在线阅读,更多相关《Java I 6时间日期数字.ppt(17页珍藏版)》请在三一办公上搜索。

1、第章时间、日期和数字,6.1 Date类6.2 Calendar Calendar类6.3 Math类6.4 BigInteger 类,.1 Date类,Date类在java.util包中。使用Date类的无参数构造方法创建的对象可以获取本地当前时间。Date对象表示时间的默认顺序是:星期、月、日、小时、分、秒、年。例如:Sat Apr 28 21:59:38 CST 2001。,可以使用DataFormat的子类SimpleDateFormat来实现时期的格式化。SimpleDateFormat有一个常用构造方法:public SimpleDateFormat(String pattern)

2、。该构造方法可以用参数pattern指定的格式创建一个对象.,pattern中应当含有一些特殊意义字符,这些特殊的字符被称做元字符,例如:y或yy 表示用2位数字输出年份;yyyy表示用4为数字输出年份。M 或MM 表示用2位数字或文本输出月份,如果想用汉字输出月份,pattern中应连续包含至少3个M,如:MMM。d 或dd 表示用2为数字输出日。H或HH 表示用两位数字输出小时。m或mm 表示用两位数字输出分。s或ss 表示用两位数字输出秒。E 表示用字符串输出星期。,计算机时间的“公元”设置在1970年1月1日0时(格林威治时间),据此可以使用Date带参数的构造方法:Date(long

3、 time)例:Date date1=new Date(1000);Date date2=new Date(-1000);此时,date1的时间就是1970年01月01日08时00分01秒,date2的时间就是1970年01月01日07时59分59秒。System类的静态方法public long currentTimeMillis()也可获得系统的当前时间。这是从1970年01月01日00时00分00秒到目前时刻所走过的毫秒数。,例:import java.util.*;import;public class TestDatepublic static void main(String ar

4、gs)Date nowtime=new Date();System.out.println(nowtime);SimpleDateFormat matter1=new SimpleDateFormat(time:yyyy年MM月dd日 E 北京时间);System.out.println(matter1.format(nowtime);SimpleDateFormat matter2=new SimpleDateFormat(北京时间:yyyy年MM月dd日HH时mm分ss秒);System.out.println(matter2.format(nowtime);Date date1=new

5、Date(1000),date2=new Date(-1000);System.out.println(matter2.format(date1);System.out.println(matter2.format(date2);System.out.println(new Date(System.currentTimeMillis();,屏幕上输出的结果是:?,6.2 Calendar类,Calendar类在java.util包中。使用Calendar类的static方法getInstance()可以初始化一个日历对象,如:Calendar calendar=calendar.getInst

6、ance();然后,calendar对象可以调用方法:set(int year,int month,int date)set(int year,int month,int date,int hour,int minute)set(int year,int month,int date,int hour,int minute,int second)将日历翻到任何一个时间,当参数year取负数时表示公元前。,calendar对象调用方法:public int get(int field)可以获取有关年份、月份、小时、星期等信息,参数field的有效值由Calendar的静态常量指定,例如:cale

7、ndar.get(Calendar.MONTH);返回一个整数,如果该整数是0表示当前日历是在一月,该整数是1表示当前日历是在二月等。日历对象调用public long getTimeInMillis()可以将时间表示为毫秒。,例:import java.util.*;import;class TestCalendarpublic static void main(String args)Calendar cal=Calendar.getInstance();cal.setTime(new Date();String year=String.valueOf(cal.get(Calendar.Y

8、EAR),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);int hour=cal.get(Calendar.HOUR_OF_DAY),minute=cal.get(Calendar.MINUTE),second=cal.get(Calendar.SECOND);(现在的时间是:);System.out.println(+year+年+mont

9、h+月+day+日+星期+week);System.out.println(+hour+时+minute+分+second+秒);cal.set(1985,5,29);/将日历翻到1985年6月29日 long time1985=cal.getTimeInMillis();cal.set(2009,9,29);long time2009=cal.getTimeInMillis();long day_num=(time2009-time1985)/(1000*60*60*24);System.out.println(2006年10月29日和1962年6月29日相隔+day_num+天);,屏幕上

10、输出的结果是:?,再例:import java.util.*;class CalendarPagepublic static void main(String args)(日 一 二 三 四 五 六);Calendar cal=Calendar.getInstance();cal.set(2009,9,1);/将日历放到09年10月1日int week=cal.get(Calendar.DAY_OF_WEEK)-1;String a=new Stringweek+31;/存放号码的一维数组for(int i=0;iweek;i+)ai=*;for(int i=week,n=1;iweek+31

11、;i+)if(n=9)ai=String.valueOf(n)+;else ai=String.valueOf(n);n+;for(int i=0;ia.length;i+)if(i%7=0)();(+ai);,输出上述日历页如何编程实现?,6.3 Math类,在编写程序时,可能需要计算一个数的平方根、绝对值、获取一个随机数等等。java.lang包中的类包含许多用来进行科学计算的类方法,这些方法可以直接通过类名调用。另外,Math类还有两个静态常量,E和PI,它们的值分别是:和。,Math类常用方法,public static double abs(double a)返回a的绝对值。publ

12、ic static double max(double a,double b)返回a、b的最大值。public static double min(double a,double b)返回a、b的最小值。public static double random()产生一个0到1之间的随机数(不包括0和1)。public static double pow(double a,double b)返回a的b次幂。public static double sqrt(double a)返回a的平方根。public static double log(double a)返回a的对数。public stati

13、c double sin(double a)返回正弦值。public static double asin(double a)返回反正弦值。,对数字结果格式化,怎样将3.1415926输出显示为:003.1416,这需要用到java.text包中的NumberFormat类。实例化NumberFormat对象调用类方法:public static final NumberFormat getInstance()格式化数字调用方法:public final String format(double numer)NumberFormat类有如下常用方法:public void setMaximum

14、FractionDigits(int newValue)public void setMinimumFractionDigits(int newValue)public void setMaximumIntegerDigits(int newValue)public void setMinimumIntegerDigits(int newValue),6.4 BigInteger类,程序有时需要处理大整数,java.math包中的BigInteger类提供任意精度的整数运算。可以使用构造方法:public BigInteger(String val)构造一个十进制的BigInteger对象。注

15、意:当字符串val中含有非数字字符时,则发生NumberFormatException异常,BigInteger类的常用方法,public BigInteger add(BigInteger val)返回当前大整数对象与参数指定的大整数对象的和。public BigInteger subtract(BigInteger val)返回当前大整数对象与参数指定的大整数对象的差。public BigInteger multiply(BigInteger val)返回当前大整数对象与参数指定的大整数对象的积。public BigInteger divide(BigInteger val)返回当前大整数

16、对象与参数指定的大整数对象的商。public BigInteger remainder(BigInteger val)返回当前大整数对象与参数指定的大整数对象的余。,BigInteger类的常用方法(续),public int compareTo(BigInteger val)返回当前大整数对象与参数指定的大整数对象的比较结果:1、-1或0。public BigInteger abs(BigInteger val)返回当前大整数对象的绝对值。public BigInteger pow(int exponent)返回当前大整数对象的exponent次幂。public String toStrin

17、g()返回当前大整数对象十进制的字符串表示。public String toString(int p)返回当前大整数对象p进制的字符串表示。例:计算1+2!+3!+.前30项之和。,例:import java.math.*;public class TestBigIntegerpublic static void main(String args)BigInteger sum=new BigInteger(0),xiang=new BigInteger(1),ONE=new BigInteger(1),i=ONE,m=new BigInteger(30);while(pareTo(m)=0)sum=sum.add(xiang);i=i.add(ONE);xiang=xiang.multiply(i);System.out.println(sum);,屏幕上输出的结果是:?,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号