您的位置:首页 > 其它

常用类:System,Runtime,Math,Date,Calendar

2016-05-03 22:40 399 查看
System 类中的方法都是静态的

常见方法:

long currentTimeMillis();

获取当前时间的毫秒值

2 Properties getProperties()

获取系统的属性信息 返回Properties集合中

Properties集合中存储的都是String类型的键和值。

最好使用它自己的存储和取出的方法来完成元素的操作

键:line.separator 代表的值为行分隔符

System.setProperty() 给系统设置一些属性信息

Runtime:没有构造方法摘要,说明该类不可以创建对象。

又发现还有非静态方法,说明该类应该提供静态的返回该类对象的方法。

而且只有一个,说明Runtime类使用单例设计模式

Runtime r= Runtime.getRuntime();

r.exec("notepad.exe") 执行文件

execute:执行 xxx.exe

Math类:方法为静态

ceil();返回大于参数的最小整数。

fioor();返回小于参数的最大整数

round();返回四舍五入的整数

pow(a,b);返回a的b次幂

double random();返回大于等于0 小于1 的伪随机数

Date类:

12个月份用0-11表示

日期对象和毫秒值之间的转换

毫秒值-->日期对象:

1 通过Date对象的构造方法new Date(timeMillies);

2 setTime方法设置

原因:可以通过Date对象的方法对日期中的各个字段(年月日等)进行操作。

日期对象-->毫秒值:

1 getTime方法

因为可以通过具体的数值进行运算

日期格式化

日期-->日期格式的字符串对象。

使用的是DateFormat类中的format方法

例:

Date date = new Date();

//获取日期格式对象。具备默认风格.FULL LONG 等可以指定风格

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);

dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);

//自定义风格如何解决?

dateFormat = new SimpleDateFormat("yyyy--MM--dd");

String str_date = dateFormat.format(date);

System.out.println(str_date);

日期格式字符串对象-->日期对象

//使用的是DateFormat类中的parse

//throws ParseException

String str_date = "2012年4月19日";

str_date = "2016---5---8";//自定义

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);

dateFormat = new SimpleDateFormat("yyyy---MM---dd");//自定义

Date date = dateFormat.parse(str_date);

System.out.println(date);

Calendar:

星期是按周日为第一个来算的

Calendar c = Calendar.getInstance();

int year = c.get(Calendar.YEAR);

int month = c.get(Calendar.MONTH)+1;

int day = c.get(Calendar.DAY_OF_MONTH);

int week = c.get(Calendar.DAY_OF_WEEK);//数字为0-8

System.out.println(year+"年"+month+"月"+day+"日");

练习:

求闰年2月的天数

Calendar c = Calendar.getInstance();

c.set(2011,2,1);//设置一个日期

c.add(Calendar.DAY_OF_MONTH, -1);//向前偏移一天

System.out.println(c);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: