JAVA 工具类

【JAVA 工具类】

System

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

out:标准输出,默认是控制台(屏幕)。

in:标准输入,默认是键盘。

获取系统属性信息:Properties getProperties();

import java.util.*;
class SystemDemo 
{
    public static void main(String[] args) 
    {
        Properties prop = System.getProperties();
        //因为Properties是Hashtable的子类,也就是Map集合的一个子类对象。那么可以通过Map的方法取出该集合中的元素。获取所有属性信息。
        for(Object obj : prop.keySet())//Map方法keySet
                //可用Properties方法stringPropertyNames代替keySet
        {
            String value = (String)prop.get(obj);
                //该集合中存储都是字符串。没有泛型定义。
            System.out.println(obj+"对应"+value);//①
        }
        System.out.println(prop);//②
        //在系统中自定义特有信息
        System.setProperty("mykey","myvalue");
        //获取指定属性信息。
        String value = System.getProperty("os.name");
        System.out.println("value="+value);
        //在jvm启动时,动态加载属性信息:                 DOS命令:java -Dhaha=sth SystemDemo
        String v = System.getProperty("haha");
        System.out.println("v="+v);//③
    }
}

运行结果


1


2


3

Runtime

该类并没有提供构造函数,说明不可以new对象。那么会直接想到该类中的方法都是静态的。

发现该类中还有非静态方法。说明该类肯定会提供了方法获取本类对象。而且该方法是静态的,并返回值类型是本类类型。

由这个特点可以看出该类使用了单例设计模式完成。

该方式是static Runtime getRuntime();

class  RuntimeDemo
{
    public static void main(String[] args) throws Exception
                //exec 应抛IOException,不想导入IO故抛其父类Exception
    {
        Runtime r = Runtime.getRuntime();
        //r.exec("E:\\Program Files (x86)\\SogouExplorer\\SogouExplorer.exe");
        //路径’\’换成’\\’,因\要转义字符,不写路径则在环境变量路径里面找程序
        Process p = r.exec("notepad.exe  SystemDemo.java");
        //通过记事本程序打开SystemDemo.java文件
        Thread.sleep(4000); //等4s
        p.destroy();//杀掉子进程
    }
}

Date

import java.util.*;
import java.text.*;
class DateDemo 
{
    public static void main(String[] args) 
    {
        Date d = new Date();
        System.out.println(d);// Sun Aug 30 17:02:27 CST 2015
        //打印的时间看不懂,希望有些格式。
        //将模式封装到SimpleDateformat对象中。
        SimpleDateFormat sdf = new SimpleDateFormat("yy年M月d日 E 全年第D天 ah:m:s:S z");//15年8月30日 星期日 全年第242天 下午5:13:48:42 CST
        //调用format方法让模式格式化指定Date对象。
        String time = sdf.format(d);
        System.out.println("time="+time);
    }
}

日期和时间模式

字母日期或时间元素表示示例
GEra 标志符TextAD
yYear1996; 96
M年中的月份MonthJuly; Jul; 07
w年中的周数Number27
W月份中的周数Number2
D年中的天数Number189
d月份中的天数Number10
F月份中的星期Number2
E星期中的天数TextTuesday; Tue
aAm/pm 标记TextPM
H一天中的小时数(0-23)Number0
k一天中的小时数(1-24)Number24
Kam/pm 中的小时数(0-11)Number0
ham/pm 中的小时数(1-12)Number12
m小时中的分钟数Number30
s分钟中的秒数Number55
S毫秒数Number978
z时区General time zonePacific Standard Time; PST; GMT-08:00
Z时区RFC 822 time zone-0800

Calendar

DAY_OF_WEEK

注意:Calendar.DAY_OF_WEEK返回的星期几,要减1才是我们通常意义上的星期几(返回的星期天对应数值1,星期一对应2,以此类推)

int java.util.Calendar.DAY_OF_WEEK
Field number for get and set indicating the day of the week. This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.

int java.util.Calendar.SUNDAY : 1 [0x1]
Value of the DAY_OF_WEEK field indicating Sunday.

int java.util.Calendar.MONDAY : 2 [0x2]
Value of the DAY_OF_WEEK field indicating Monday.

MONTH

注意:Calendar.MONTH返回的月份,要加1才是我们通常意义上的月份(返回的一月对应数值0,二月对应1,以此类推)

int java.util.Calendar.MONTH
Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

import java.util.*;
import java.text.*;
import static java.util.Calendar.*;//静态导入,下面的Calendar.都可去掉了

class CalendarDemo {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        System.out.println(c.get(Calendar.YEAR) + "年");//->2018年
        System.out.println((c.get(Calendar.MONTH) + 1) + "月");//->1月
        System.out.println(c.get(Calendar.DAY_OF_MONTH) + "日");//->15日
        System.out.println("星期" + (c.get(Calendar.DAY_OF_WEEK) - 1));//->星期1
        //查表法
        String[] mons = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
        String[] weeks = { "", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        int index = c.get(MONTH);//因为有静态导入,可以不用写Calendar.MONTH
        int index1 = c.get(DAY_OF_WEEK);
        System.out.println(mons[index]);//->一月
        System.out.println(weeks[index1]);//->星期一
    }
}

Calendar.set

void java.util.Calendar.set(int year, int month, int date)
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH. Previous values of other calendar fields are retained. If this is not desired, call clear() first.
Parameters:
 year:the value used to set the YEAR calendar field.
 month:the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.
 date:the value used to set the DAY_OF_MONTH calendar field.

import java.util.*;

class CalendarDemo2 {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.set(2015, 5, 26);
        printCalendar(c); //->2015年六月26日星期五
        c.add(Calendar.MONTH, -1);
        printCalendar(c);//->2015年五月26日星期二
    }

    public static void printCalendar(Calendar c) {
        String[] mons = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
        String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        int index = c.get(Calendar.MONTH);
        int index1 = c.get(Calendar.DAY_OF_WEEK) - 1;
        System.out.print(c.get(Calendar.YEAR) + "年");
        System.out.print(mons[index]);
        System.out.print(c.get(Calendar.DAY_OF_MONTH) + "日");
        System.out.print(weeks[index1]);
        System.out.println();
    }
}

Calendar.add VS .roll

class CalendarDemo3 {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.set(2000, 0, 1);
        printCalendar(c);//->2000年1月1日星期6
        c.add(Calendar.MONTH, 15);
        printCalendar(c); //->2001年4月1日星期0(通常意义上的星期天)

        c.set(2000, 0, 1);
        c.roll(Calendar.MONTH, 15);
        printCalendar(c); //->2000年4月1日星期6

        c.set(2000, 0, 0);
        printCalendar(c);//->1999年12月31日星期5
    }

    public static void printCalendar(Calendar c) {
        System.out.println(c.get(Calendar.YEAR) + "年" 
                + (c.get(Calendar.MONTH) + 1) + "月" 
                + c.get(Calendar.DAY_OF_MONTH)+ "日" 
                + "星期" + (c.get(Calendar.DAY_OF_WEEK) - 1));
    }
}

习题: 获取任意年的二月有多少天

思路:
- 方式一:
根据指定年设置一个时间:c.set(year,2,1)//某一年的3月1日。
c.add(Calendar.DAY_OF_MONTH,-1);//3月1日,往前推一天,就是2月最后一天。
- 方式二:
根据指定年设置一个时间:c.set(year,2,0)//某一年的3月1日的前一天,也就是2月最后一天。

class CalendarTest {
    public static void main(String[] args) {
        febDays(2016);
        febDays2(2016);
    }

    public static void febDays(int y) {
        Calendar c = Calendar.getInstance();
        c.set(y, 2, 1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        int d = c.get(Calendar.DAY_OF_MONTH);
        System.out.println(y + "年2月有" + d + "天");
    }

    public static void febDays2(int y) {
        Calendar c = Calendar.getInstance();
        c.set(y, 2, 0);
        int d = c.get(Calendar.DAY_OF_MONTH);
        System.out.println(y + "年2月有" + d + "天");
    }
}

Math

import java.util.*;
class  MathDemo
{
    public static void main(String[] args) 
    {
        double d = Math.ceil(16.34);//ceil返回大于指定数据的最小整数。
        double d1 = Math.floor(12.34);//floor返回小于指定数据的最大整数。
        long l = Math.round(12.54);//四舍五入
        sop("d="+d);// d=17.0
        sop("d1="+d1);// d1=12.0
        sop("l="+l);// l=13
        double d2 = Math.pow(2,3);//返回第一个参数的第二个参数次幂的值
        sop("d2="+d2);// d2=8.0
        Random r=new Random();
        for (int x=0; x<10; x++)
        {
            int d=(int)(Math.random()*10+1);//[1,10]随机数
            //int d=r.nextInt(10)+1;//[1,10]随机数
            sop(d);
        }
    }
    public static void sop(Object obj)
    {
        System.out.println(obj);
    }
}

random

public static double random()
返回: 大于等于 0.0 且小于 1.0 的伪随机 double 值。

double java.lang.Math.random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random()
This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.
This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.
Returns:a pseudorandom double greater than or equal to 0.0 and less than 1.0.
See Also:Random.nextDouble()

nextInt(int n) //Random类方法
返回:一个伪随机数,它是取自此随机数生成器序列的、在0(包括)和指定值(不包括)之间均匀分布的 int 值。

习题:给定一个小数,保留该小数的后指定位数

import java.util.*;
public class MathTest {
    public static void main(String[] args) {
        saveAs(12.3456, 3, true);//12.346
        saveAs(12.3456, 3, false);//12.345
    }

    //给定一个小数d, 保留小数点后scale位, 是否四舍五入
    public static void saveAs(double d, int scale, boolean isRound) {
        double base = Math.pow(10, scale);
        double num = isRound ?
                Math.round(d * base) / base 
                : ((int) (d * base)) / base;
        System.out.println("num=" + num);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值