一、System类
System 类包含一些有用的类字段和方法。它不能被实例化。
1、描述系统属性
(1)、static Properties getProperties() 确定当前的系统属性。 Properties
(2)、static String getProperty(String key, String value)
设置指定键指示的系统属性。
(3)、static void setProperties(Properties props)
将系统属性设置为 Properties 参数。
public static void main(String[] args) throws IOException
{
Properties p=System.getProperties();
//通过MAP集合中的方法获取集合中的元素
for(Object obj:prop.keySet())
{
String value=(String)prop.get(obj);
System.out.println(obj+"::"+value);
}
String value=System.getProperty("os.name");
System.out.println(value);
}
二、Runtime类
每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。
static Runtime getRuntime() 获取当前 Java 应用程序相关的运行时对象。
Runtime r=Runtime.getRuntime();
Process p=r.exec("winmine.exe");//运行进程
p.destory();//杀掉子进程
Process p1=r.exec("notepad systemDemo.java");//在记事本中打开文件
三、Date、Calendar类
1.Date
比较古老的一个类, 大多数方法已过时, 但通常我们还会用它来获取当前时间,new Date()可以创建日期对象, 然后使用SimpleDateFormat可以将其格式化成我们需要的格式,通常使用的格式为: “yyyy-MM-dd HH:mm:ss”。
(1)、获取当前时间的毫秒值 long getTime()
Date d = new Date();
d.getTime(); //获取的是1970年1月1日0时0分0秒到当前时间的毫秒值
System.currentTimeMillis();
(2)、将毫秒值转换成时间对象
Date d = new Date(毫秒值) //通过毫秒值获取时间对象
Date d = new Date(); //创建时间对象
d.setTime(毫秒值); //根据毫秒值修改时间对象
2.Calendar
很多方法都是替代了Date类的方法, 最常用的就是
int get(int field)(Calendar.YEAR) //通过传入的字段获取对应的值,(获取年对应的值)
void add(int field, int amount) //field代表传入的时间字段可以是年月日等,amount代表是数值,正数就是在传入的字段上加,负数减
void set(int field, int value) //field代表传入的时间字段可以是年月日等,value代表设置的值,想设置哪一年或月日等,就写哪个值
void set(int year, int month, int date)
可以对指定的字段获取, 设置, 以及增减 六.Math
提供了一些和数学运算相关的方法,
static double PI //获取π(派)的值
static double floor(double a) //是小于等于a这个double值的最大整数对应的double值
static double ceil(double a) //是大于等于a这个double值的最小整数对应的double值
static long round(double a ) //四舍五入,返回是一个long值
static double sqrt(double a) //开平方
static double pow(double a, double b) //a是底数,b是指数返回的是a的b次幂
import java.util.*;
import java.text.*;
class DateDemo
{
public static void main(String[] args)
{
/*
//由于计算机记录月份是从0开始的,而且西方的记录星期的方式和我们也不一样,他们的第一天是星期日
//所以要显示我们的正确的日期和星期要加以改进
Calendar c=Calendar.getInstance();
//运用查表法
String[] month={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int index=c.get(Calendar.MONTH);
System.out.println(month[index]);
String[] week={"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int index1=c.get(Calendar.DAY_OF_WEEK);
System.out.println(week[index1]);
*/
/*
Calendar c=Calendar.getInstance();
System.out.println("年"+c.get(Calendar.YEAR));
System.out.println("月"+c.get(Calendar.MONTH));
System.out.println("日"+c.get(Calendar.DAY_OF_MONTH));
System.out.println("星期"+c.get(Calendar.DAY_OF_WEEK));
*/
/*回去年月日时分秒
Date d=new Date();
//System.out.println("Date:"+d);
SimpleDateFormat sim=new SimpleDateFormat("yyyy:MM:dd kk:mm:ss");
String time=sim.format(d);
System.out.println("time:"+time);
*/
/*
//1 仅仅只获取年份
Date d=new Date();
//System.out.println("Date:"+d);
SimpleDateFormat sim=new SimpleDateFormat("yyyy年");
String year=sim.format(d);
System.out.println("year:"+year);
*/
}
}
//获取任意年的二月有多少天
import java.util.*;
class DateTest
{
public static void main(String[] args)
{
getSecondDay(2000);
getLastDayTime();
}
public static void getLastDayTime()//获取昨天的这个时刻
{
Calendar c=Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH,-1);
printTime(c);
}
public static void getSecondDay(int year)
{
Calendar c=Calendar.getInstance();
c.set(year,2,1);
c.add(Calendar.DAY_OF_MONTH,-1);
printTime(c);
}
public static void printTime(Calendar c)
{
String[] month={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int index=c.get(Calendar.MONTH);
System.out.println(c.get(Calendar.YEAR)+"年");
System.out.println(month[index]);
System.out.println(c.get(Calendar.DAY_OF_MONTH)+"天");
String[] week={"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int index1=c.get(Calendar.DAY_OF_WEEK);
System.out.println(week[index1]);
}
}