import java.util.*; /* System类: 获取系统属性 static Properties getProperties() 确定当前的系统属性。 */ class SystemTest { public static void main(String[] args) { //获取系统信息 Properties pro = System.getProperties(); /* //输出全部 for(Object obj : pro.keySet()) { out(obj); } */ //获取制定键值 String value = System.getProperty("os.name"); out("os name:" + value); //设置值 System.setProperty("haha","hehe"); out(System.getProperty("haha")); //还可以通过命令行设置值 //格式:java -Dkey=value out(System.getProperty("myname")); } //输出 public static void out(Object obj) { System.out.println(obj); } }
/* Rumtime对象 该类没有构造函数。 通过Runtime.getRuntime()方法获取一个Runtime对象 可以看出该类使用了单例设计模式 */ class RuntimeTest { public static void main(String[] args) throws Exception { //获取Runtime对象 Runtime r = Runtime.getRuntime(); //启动一个程序,并打开该文件 Process p = r.exec("notepad RuntimeTest.java"); //5秒钟后杀掉 Thread.sleep(4000); p.destroy(); } }
import java.util.*; import java.text.*; /* 格式化当前日期 */ class DateTest { public static void main(String[] args) { Date d = new Date(); out(d); //格式化日期字符串 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E hh:mm:ss"); out(sdf.format(d)); } //输出 public static void out(Object obj) { System.out.println(obj); } }
import java.util.*; /* Calendar类 1、输出当前日期 2、获取任意一年的2月有多少天 思路:将该年和3月1一设置为当前日前,然后对当前日期减一 然后获取DAY_OF_MONTH 3、输出昨天的现在时刻(减一天) */ class CalendarTest { public static void main(String[] args) { Yesterday(); } //输出日期 public static void outDate(Calendar c) { // //日历实例 // Calendar c = Calendar.getInstance(); String[] months = {"一","二","三","四" ,"五","六","七","八" ,"九","十","十一","十二"}; String[] days = {"","日","一","二","三" ,"四","五","六"}; int week = c.get(Calendar.DAY_OF_WEEK); //1是星期日 int month = c.get(Calendar.MONTH); out(c.get(Calendar.YEAR)+"年" + months[month] + "月" + c.get(Calendar.DAY_OF_MONTH) + "日" + " 星期" + days[week] + " " + c.get(Calendar.HOUR_OF_DAY) + "时" + c.get(Calendar.MINUTE) + "分" + c.get(Calendar.SECOND) + "秒" + c.get(Calendar.MILLISECOND) + "毫秒"); } //2 public static void Test() { Calendar c = new GregorianCalendar(); Scanner in = new Scanner(System.in); out("请输入一个年份:(如:1995)"); int temp = in.nextInt(); c.set(temp,2,1); c.add(Calendar.DAY_OF_MONTH, -1); out(temp + "年的二月有" + c.get(Calendar.DAY_OF_MONTH) + "天"); } //3 public static void Yesterday() { Calendar c = new GregorianCalendar(); c.add(Calendar.DAY_OF_MONTH, -1); System.out.print("昨天的现在时间是:"); outDate(c); } //输出 public static void out(Object obj) { System.out.println(obj); } }
import java.util.*; import static java.lang.Math.*; /* Math类方法 double ceil() 返回该数的最小整数(向上取整) double floor() 返回该数的最大整数(向下取整) double random() 返回0.0 到 1.0, 不包括1 double pow(double a, double b) a的b次幂 int round(float a) 四舍五入 */ class MathTest { public static void main(String[] args) { randomTest(); } //Math public static void MathMethod() { out(ceil(12.5)); out(floor(12.6)); out((int)(random() * 10)); out(pow(2, 5)); out(round(12.7)); out(round(12.3)); } //java.util.Random类 public static void randomTest() { Random rand = new Random(); out(rand.nextInt(10)); } //输出 public static void out(Object obj) { System.out.println(obj); } }
20