Java第二十天学习笔记~其他对象API(System类、Runtime类、Math类、Date类、Calendar类)、IO流

System类

类中的方法和属性都是静态的

常见方法:Long currentTimeMillis( );获取当前时间毫秒值

 Properties prop=System.getProperties( );获取系统的属性信息,并存储到Properties集合中

Runtime类

使应用程序与其运行的环境相连

应用程序不能创建自己的Runtime

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

又发现还有非静态方法,说明该类应该是提供静态的返回该类对象的方法,而且只有一个,说明Runtime类使用了单例设计模式

Runtime getRuntime();
获取Runtime类的对象

Process exec(String command);
执行命令 execute

destroy();
杀掉子进程

Math类

提供了操作数学运算的方法,都是静态的

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

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

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

pow(a,b,);a的b次方

random();随机数,大于0小于1

Random类是用于生成伪随机数的

Date类

时间和日期的对象

日期对象和毫秒值之间的转换
毫秒值->日期对象
1.通过Date对象的构造方法 new Date(Millis);
2.通过Date 中的setTime(Millis)方法设置

日期对象->毫秒值
getTime方法
因为可以通过具体的数值进行运算

对日期对象进行格式化

日期对象->日期对象的字符串
使用的是DateFormate类中的formate方法
先获取日期格式化对象,然后将date作为参数对象传入formate函数,转为日期对象的字符串类型

DateFormate是抽象类不能创建子类,所以使用getInstance方法获取对象

日期格式的字符串->日期对象
使用的是DateFormate类中的parse方法

SimpleDateFormat类可以对日期时间对象自定义格式

Calendar类

c.add(Calendar.Year,2 );向右偏移2年 +2年 

package cn.itcast.p1.otherapi.test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//练习1:
//* "2012-3-17"到"2012-4-6"
//* 中间有多少天。
/*
* 1.首先把两个时间字符串转为时间对象。
* 2.将时间对象转为毫秒值。
* 3.两个毫秒值进行相减,得到两个毫秒值之差。
* 4.将毫秒值变为天数。
*
* */
public class DateTest {
	public static void main(String[] args){
	String str_date1 = "2012-3-17";
	String str_date2 = "2012-4-6";
	
	int day = getDay(str_date1,str_date2);
	System.out.println(day);
	
	
	}

	public static int getDay(String str_date1, String str_date2) {
	//1.将时间字符串转为时间对象。
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	try {
	  Date date1 = dateFormat.parse(str_date1);
	  Date date2 = dateFormat.parse(str_date2);
	  //将时间对象转为毫秒值;
	  long time1 = date1.getTime();
	  long time2 = date2.getTime();
	  //得到毫秒值之差
	  long time = Math.abs(time2-time1);
	  //转为天数。
	  int day = transferDay(time);
	
	  return day;
	} catch (ParseException e) {
	  e.printStackTrace();
	}
	
	return -1;
	}
	
	private static int transferDay(long time) {
	return (int)(time/1000/60/60/24);
	}
}

 

add(int field, int amount);
根据日历的规则,为给定的日历字段添加或者减去指定的时间量

get(int field);
返回给定日历的字段值

set();
设置年月日时间等
它也是抽象类,要用getInstance方法获取对象
package cn.itcast.p1.otherapi.test;

import java.util.Calendar;

/*
练习2;
得到每个月有多少天
*/
public class DateTest2 {
    public static void main(String[] agrs){
        //创建对字段操作的对象
        Calendar cal = Calendar.getInstance();

        int day = getDayOfMonth(2013,1);
//        method(cal);
        System.out.println(day);


    }

//对时间字段进行的操作,主要是输出年月日和星期。
    private static void method(Calendar cal) {
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;
        int day = cal.get(Calendar.DAY_OF_MONTH);

        String week = getWeek(cal.get(Calendar.DAY_OF_WEEK));
        System.out.println(year+"年"+month+"月"+day+"日"+week);

    }

    private static String getWeek(int i) {
        String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};

        return weeks[i];
    }

//得到某一月的天数,
    public static int getDayOfMonth(int year,int month) {
        Calendar cal = Calendar.getInstance();

        cal.set(year,month,1);
        cal.add(Calendar.DAY_OF_MONTH,-1);
        method(cal);
        return cal.get(Calendar.DAY_OF_MONTH);

    }
}

 IO流

1.输入输出流,是相对于设备而言的;简写模式为IO流

将外设中的数据读入内存中:输入,读

将内存中的数学写入外设中:输出,写

按操作的数据分为字节流和字符流

字节流的两个顶层父类:InputStream,OutputStream

字符流的两个顶层父类:Reader输入,Writer输出

这些体系的子类都以父类名作为后缀

前缀名就是该对象的功能

2.字符流

其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表,获取对应的文字。再对文字进行操作,简单的来说就是:字节流+编码表=字符流。

package cn.itcast.p2.io.filewriter;

import java.io.FileWriter;
import java.io.IOException;

/*需求1:将一些文字存储到硬盘的一个文件中。
如果要操作文字数据,优先考虑字符流。
*/


public class FileWriterDemo {
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");

    public static void main(String[] args) throws IOException {
        //创建一个可以往文件中写入字符数据的输出流对象。
        /*
        * 既然是往一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据的目的地).
        *
        * 如果文件不存在则会自己创建,如果文件存在则会覆盖。
        *
        * 如果构造函数中加入true,可以实现对文件的续写。
        * */

        FileWriter fw = new FileWriter("demo.txt",true);

        /*
        * 调用Writer中的write(String)方法,写入数据。
        *
        * 其实数据写到了临时存储缓冲区中。
        *
        * */
        fw.write(LINE_SEPARATOR+"abcdef"+"ajksjaksaksa");
        /*
        * 进行刷新,将数据直接写到目的地。
        * */
//        fw.flush();
        /*
        * 关闭流,关闭资源。
        *在关闭前,会调用flush刷新缓冲区的数据到目的地。
        * */

        fw.close();
//        fw.write("haha");//关闭流后不能写入,否则抛出java.io.IOException
    }
}


 

猜你喜欢

转载自blog.csdn.net/crazyhulu/article/details/85087857