Java-其余对象API

System类

  • System:类中的方法和属性都是静态的。

常用方法

currentTimeMillis

  • 获取当前时间的毫秒值。
1
public static long currentTimeMillis(); 与1970.1.1午夜时间的差

Properties

  • 获取系统的属性信息,并存储到了Properties集合中。
  • properties集合中存储都是String类型的键和值。
  • 最好使用它自己的存储和取出的方法来完成元素的操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
public static void demo_1(){

Properties prop = System.getProperties();

Set<String> nameSet = prop.stringPropertyNames();

for(String name : nameSet){
String value = prop.getProperty(name);

System.out.println(name+"::"+value);
}

}
  • 在linux、windows上换行符号是不一样的
1
2
3
4
5
6
//如果这样写的话,这个换行符在哪个系统中都不会出问题
System.out.println(System.getProperty("hello"+"line_separator"+"world");

//如果多次调用太麻烦,就可以将他在文件中用final定义
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
System.out.println("hello"+LINE_SEPARATOR+" world");

Runtime类

  • Runtime:没有构造方法摘要,说明该类不可以创建对象。
  • 又发现还有非静态的方法。说明该类应该提供静态的返回该类对象的方法
  • 而且只有一个,说明Runtime类使用了单例设计模式
1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) throws IOException, InterruptedException {

Runtime r = Runtime.getRuntime();

// execute: 执行。 xxx.exe

Process p = r.exec("notepad.exe");
// r.exec("D:\\abc\\asd\\xxxx.exe");
Thread.sleep(5000); //延迟
p.destroy(); //杀死进程,只能杀掉他开启的
}

Math类

  • Math:提供了操作数学运算的方法。都是静态的
  • 常用的方法:
    • ceil():返回大于参数的最小整数。(如18.6 返回19)
    • floor():返回小于参数的最大整数。(如18.6 返回18)
    • round():返回四舍五入的整数。 (如18.6 返回19)
    • pow(a,b):a的b次方。
    • random():返回大于0.0且小于1.0的一个伪随机数
1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i = 0; i < 10; i++) {
double d1 = Math.ceil(Math.random()*10); //返回 1 到 10 的数值
double d2 = Math.ceil(Math.random()*10); //返回 0 到 9 的数值
double d3 = (int)(Math.random()*10+1); //返回 1 到 10 的数值
System.out.println(d1);
}
//Random也有自己的对象
Random r = new Random();
for (int i = 0; i < 10; i++) {
double d4 = (int)(r.nextDouble()*6+1);
int d5 = r.nextInt(6); //限定数值大于0 小于6
System.out.println(d5);
}

Date类

  • 月份:0–11 (代表12个月)

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

  • 毫秒值–>日期对象 :
    • 1,通过Date对象的构造方法 new Date(timeMillis);
    • 2,还可以通过setTime设置。
    • 因为可以通过Date对象的方法对该日期中的各个字段(年月日等)进行操作。
  • 日期对象–>毫秒值:
    • 2,getTime方法。
    • 因为可以通过具体的数值进行运算。
1
2
3
System.out.println(System.currentTimeMillis());
Date d = new Date(1526459420372L); //因为数值太大,所以加上L
System.out.println(d);

将日期对象进行格式化

  • 将日期对象–>日期格式的字符串
  • 使用的是DateFormat类中的format方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void methodDemo() {

Date date = new Date();

//获取日期格式对象。具体着默认的风格。 FULL LONG等可以指定风格。
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
// System.out.println(dateFormat);

//如果风格是自定义的如何解决呢?
dateFormat = new SimpleDateFormat("yyyy--MM--dd");

String str_date = dateFormat.format(date);

System.out.println(str_date);
}
  • 将日期格式的字符串–>日期对象
  • 使用的是Date Format类中的parse()方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
public  static void methodDemo_3() throws ParseException {

String str_date1 = "2018年5月16日";
DateFormat dateFormat1 = DateFormat.getDateInstance(DateFormat.LONG);

String str_date2 = "2018---6---6";
DateFormat dateFormat2 = new SimpleDateFormat("yyyy---MM---dd");

Date date1 = dateFormat1.parse(str_date1);
Date date2 = dateFormat2.parse(str_date2);
System.out.println(date1);
System.out.println(date2);
}

练习

  • 计算2018-04-11与2018-05-16中间相差多少天
  • 思路:
    • 日期字符串转日期对象
    • 日期对象转毫秒值
    • 毫秒值相减,取绝对值
    • 将结果转换成天数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class DateTest {
public static void main(String[] args) throws ParseException {
String str1 = "2018-04-11";
String str2 = "2018-05-16";
int day = datetest(str1, str2);
System.out.println(day);
}
public static int datetest(String str1,String str2) throws ParseException {
DateFormat dateFormat = DateFormat.getDateInstance();
Date date1 = dateFormat.parse(str1);
Date date2 = dateFormat.parse(str2);
long date = Math.abs(date2.getTime()-date1.getTime());
int day = getDay(date);
return day;
}
public static int getDay(long date){
int day = (int)(date/1000/60/60/24);
return day;
}
}

calendar类

  • Date类中已经过时的类,都是在calendar中被替换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void showDate(Calendar c) {
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1; //表示月份的数字是0-11
int day = c.get(Calendar.DAY_OF_MONTH);
int week = c.get(Calendar.DAY_OF_WEEK); //这里的周日为美国的周一
System.out.println(year+"年"+month+"月"+day+"日"+getWeek(week));
}

public static String getWeek(int i) {

String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

return weeks[i];
}
  • calendar中的add方法,偏移日期
1
2
3
Calendar c = Calendar.getInstance();
c.set(2018, 11, 20);
c.add(Calendar.MONTH, 2);