第二十九讲 常用类
发布日期:2021-06-30 18:03:12 浏览次数:2 分类:技术文章

本文共 9041 字,大约阅读时间需要 30 分钟。

System

概述

System类用于描述系统一些信息,该类中的方法和属性都是静态的。其中有两个最常见的属性,分别是:

  • out:标准输出流,默认对应的设备是显示器;
  • in:标准输入流,默认对应的设备是键盘。

最常见的成员方法

方法 说明
public static long currentTimeMillis() 返回以毫秒为单位的当前时间
public static Properties getProperties() 获取当前的系统属性信息
public static String getProperty(String key) 获取指定键指示的系统属性

System类的currentTimeMillis()方法可统计一段程序的运行时间,例如:

package cn.liayun.otherapi;public class SystemDemo {
public static void main(String[] args) {
long start = System.currentTimeMillis(); for (int i = 0; i < 100; i++) {
System.out.println("hello" + i); } long end = System.currentTimeMillis(); System.out.println("共耗时:" + (end - start) + "毫秒"); }}

System类的getProperties()方法将获取到的系统信息都存储到了Properties键值集合中,又因为Properties是HashTable的子类,也就是Map集合的一个子类对象,所以可以通过Map的方法取出该集合的元素。该集合中存储的都是字符串,没有泛型定义。以下代码用于获取系统属性信息:

Properties prop = System.getProperties(); // 获取到的系统信息都存储到了Properties键值集合当中了。
  • 获取所有属性信息
    Set
    keySet = prop.stringPropertyNames(); for (String key : keySet) {
    String value = prop.getProperty(key); System.out.println(key + "::" + value);}
  • 获取指定属性信息
    String value = System.getProperty("os.name");System.out.println("value="+value);

Runtime

概述

每个Java应用程序都有一个Runtime类实例,使应用程序能够与其运行的环境相连接。可以通过 getRuntime方法获取当前运行时。应用程序不能创建自己的Runtime类实例。

最常见的成员方法

方法 说明
public static Runtime getRuntime() 返回与当前Java应用程序相关的运行时对象
public Process exec(String command) 在单独的进程中执行指定的字符串命令

Runtime类并没有提供构造函数,说明不可以new对象,那么我们会直接想到该类中的方法都是静态的,但发现该类中还有非静态方法,说明该类肯定会提供方法获取本类对象,而且该方法是静态的,返回值类型是本类类型。查阅API,可知该方法是public static Runtime getRuntime(),由这个特点可以看出该类使用了单例设计模式。

package cn.liayun.otherapi;import java.io.IOException;public class RuntimeDemo {
public static void main(String[] args) throws IOException {
/* * 单例模式的对象Runtime。 */ Runtime r = Runtime.getRuntime(); // r.exec("notepad.exe"); r.exec("notepad.exe D:\\Practise\\Java Base\\JavaSE_Code\\lee_day19\\SysInfo.txt"); }}

Math

概述

Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

最常见的成员方法

方法 说明
public static int abs(int a) 返回int值的绝对值
public static double ceil(double a) 返回大于指定数据的最小整数
public static double floor(double a) 返回小于指定数据的最大整数
public static long round(double a) 四舍五入
public static double pow(double a,double b) a的b次幂
public static double random() 返回带正号的double值,该值大于等于0.0且小于1.0
package cn.liayun.otherapi;import java.util.Random;public class MathDemo {
public static void main(String[] args) {
/* * Math:数学类,都是静态的成员。 */ double d1 = Math.ceil(12.34); double d2 = Math.floor(12.34); double d3 = Math.round(12.34);//四舍五入。 System.out.println("d1 = " + d1);//13 System.out.println("d2 = " + d2);//12 System.out.println("d3 = " + d3);//12 System.out.println(Math.pow(10, 3)); for (int x = 0; x < 10; x++) {
int d = (int) Math.ceil(Math.random() * 6); System.out.println(d); } }}

练习

给定一个小数,保留该小数的后两位。

package cn.liayun.otherapi;import java.text.DecimalFormat;public class Test {
public static void main(String[] args) {
String str_num = getNumber(0.9); System.out.println(str_num); } private static String getNumber(double d) {
DecimalFormat decimalFormat = new DecimalFormat("#0.00");//模式不能写成"#.00" return decimalFormat.format(d); }}

Random

概述

此类用于产生伪随机数,如果用相同的种子创建两个Random实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。

最常见的成员方法

方法 说明
public int nextInt() 返回的是int范围内的随机数
public int nextInt(int n) 返回的是[0,n)范围内的随机数

例,产生10个1~6(包括1,但不包括6)围内的伪随机数。

package cn.liayun.otherapi;import java.util.Random;public class RandomDemo {
public static void main(String[] args) {
Random r = new Random(); for (int i = 0; i < 10; i++) {
int num = r.nextInt(6) + 1; System.out.println(num); } }}

Date

格式化和解析日期字符串

日期对象的格式化

首先创建Date对象,并打印。

Date d = new Date();System.out.println(d); // 打印的时间看不懂,希望有些格式。

这时可发现打印的时间看不懂,想要把日期按照我们的习惯格式化一下,咋办呢?可将模式封装到SimpleDateFormat对象中,如下:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");

然后调用format()方法让模式格式化指定的Date对象,将日期对象转成日期格式的字符串。

String str_date = dateFormat.format(date);System.out.println(str_date);

日期字符串的解析

将日期格式的字符串转成日期对象,即解析。

package cn.liayun.otherapi;import java.text.DateFormat;import java.text.ParseException;import java.util.Date;public class DateDemo2 {
public static void main(String[] args) throws ParseException {
/* * "日期格式的字符串:2013-07-17" * 需要将日期格式的字符串转成日期对象,即解析。 */ String str_date = "2013-07-17"; DateFormat dateFormat = DateFormat.getDateInstance(); Date date = dateFormat.parse(str_date); System.out.println(date); }}

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

毫秒值转成日期对象

毫秒值转成日期对象,需要调用Date类的构造函数或者setTime()方法。

package cn.liayun.otherapi;import java.text.DateFormat;import java.util.Date;public class DateDemo3 {
public static void main(String[] args) {
long time = 1541179812628l; //毫秒值--->日期对象,构造函数或者setTime()方法。 Date date = new Date(time); DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); String str_date = dateFormat.format(date); System.out.println(str_date); }}

日期对象转成毫秒值

日期对象转成毫秒值,需要调用Date类的getTime()方法。

package cn.liayun.otherapi;import java.text.DateFormat;import java.util.Date;public class DateDemo3 {
public static void main(String[] args) {
long time = 1541179812628l; //毫秒值--->日期对象,构造函数或者setTime()方法。 Date date = new Date(time); DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); String str_date = dateFormat.format(date); System.out.println(str_date); //日期对象--->毫秒值,Date对象的getTime()方法。 long time2 = date.getTime(); System.out.println(time2); }}

Calendar

概述

Calendar类是一个抽象类,它为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

最常见的成员方法

方法 说明
public static Calendar getInstance() 获取实例
public int get(int field) 返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型
public abstract void add(int field, int amount) 根据日历的规则,为给定的日历字段添加或减去指定的时间量
public final void set(int year, int month, int date) 设置当前日历的年月日
package cn.liayun.otherapi;import java.util.Calendar;import cn.liayun.exception.NotWeekException;public class CalendarDemo {
public static void main(String[] args) {
/* * 演示日历。 * Calendar */ Calendar c = Calendar.getInstance(); //对日历对象进行其中日期的定义。// c.set(Calendar.YEAR, 2014);// c.set(2012, 4, 1); //日期的偏移。// c.add(Calendar.MONTH, 5); //获取其中的所需日期。 showDate(c); } private static void showDate(Calendar c) {
int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH) + 1; int day = c.get(Calendar.DAY_OF_MONTH); String week = getWeek(c.get(Calendar.DAY_OF_WEEK)); System.out.println(year + "年" + month + "月" + day + "日" + week); } private static String getWeek(int i) {
if (i > 7 || i <= 0) {
throw new NotWeekException("没有对应的星期,必须是1~7"); } String[] weeks = {
"", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; return weeks[i]; }}

cn.liayun.exception包中的NotWeekException异常类继承自RuntimeException类,并显示调用其中所有的构造方法。

package cn.liayun.exception;public class NotWeekException extends RuntimeException {
public NotWeekException() {
super(); // TODO Auto-generated constructor stub } public NotWeekException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3); // TODO Auto-generated constructor stub } public NotWeekException(String arg0, Throwable arg1) {
super(arg0, arg1); // TODO Auto-generated constructor stub } public NotWeekException(String arg0) {
super(arg0); // TODO Auto-generated constructor stub } public NotWeekException(Throwable arg0) {
super(arg0); // TODO Auto-generated constructor stub } }

练习

练习一、获取任意一年的二月有多少天。

思路:

  1. 根据指定年设置一个时间就是c.set(year, 2, 1),某一年的3月1日;
  2. c.add(Calendar.DAY_OF_MONTH, -1),3月1日往前推一天,就是2月最后一天。
package cn.liayun.otherapi;import java.util.Calendar;public class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance(); //任意一年二月有多少天?三月一日的前一天就是。 int year = 2018; c.set(year, 2, 1); c.add(Calendar.DAY_OF_MONTH, -1); //获取其中的所需日期。 System.out.println(c.get(Calendar.DAY_OF_MONTH)); }}

练习二、“2012/3/17”、"2012-5-28"这两个字符串代表的日期之间间隔多少天?

package cn.liayun.test;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class Test {
public static void main(String[] args) throws ParseException {
getDays(); } //"2012/3/17","2012-5-28",问,间隔多少天? /* * 1. 只有毫秒值可以相减。 * 2. 获取毫秒值,字符串--->日期对象--->毫秒值 */ public static void getDays() throws ParseException {
String str_date1 = "2012/5/17"; String str_date2 = "2012-5-18"; //如何将日期格式字符串解析成日期对象呢? //1.自定义yyyy/MM/dd风格对象。 DateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd"); //2.定义一个默认的风格。 DateFormat dateFormat2 = DateFormat.getDateInstance(DateFormat.MEDIUM); //3.对日期格式字符串进行解析。 Date date1 = dateFormat1.parse(str_date1); Date date2 = dateFormat2.parse(str_date2); // System.out.println(date1);// System.out.println(date2); //4.通过日期对象获取毫秒值。 long time1 = date1.getTime(); long time2 = date2.getTime(); //5.相减。 long time = Math.abs(time1 - time2); int day = transDay(time); System.out.println(day); } private static int transDay(long time) {
return (int)(time / 1000 / 60 / 60 / 24); }}

转载地址:https://liayun.blog.csdn.net/article/details/83722762 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:第三十讲 集合框架——集合类与Collection接口
下一篇:第二十八讲 常用类——基本数据类型对象包装类

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月22日 11时56分49秒