常用API

88 阅读3分钟

一.MATH

数字操作类

  • 1.取绝对值:Math.abs
  • 2.向上取整:Math.ceil
  • 3.向下取整:Math.floor
  • 4.四舍五入:Math.round
  • 5.取较大值:Math.max
  • 6.取最小值:Math.min
  • 7.取次方:Math.pow
  • 取随机数(用的少)Math.random

二.System

1、获取系统的全部信息 public static long currentTimeMillis();(重点) 获取当前系统的时间毫秒值(从1970-1-1 00:00:00 走到此刻总的毫秒值) long time = System.currentTimeMillis(); 做性能分析,做时间运算! SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss EEE a"); System.out.println(sdf.format(time));

     2、获取系统全部属性信息
    Properties properties = System.getProperties();
    System.out.println(properties);

    3、干掉JVM虚拟机(程序全部死亡了,不要用,跟删库跑路一个性质~~)
    System.exit(0); 0是正常终止!

     4、进行数组拷贝(了解)
    int[] arr1 = {10, 20, 30, 40, 50, 60, 70, 80};
    int[] arr2 = new int[6]; // arr2 = [0, 0, 0, 0, 0, 0] ==> [0, 0, 50, 60, 70, 0]
 arraycopy(Object src,int  srcPos, Object dest, int destPos,int length)
      参数一:原数组
      参数二:从哪个索引位置开始拷贝
      参数三:目标数组
      参数四:目标数组粘贴元素的起始索引位置。
      参数五:拷贝几个元素

三.BigDecimal

作用:封装大数据(Double)完成一些精度运算的。

  • 使用第一步:
  • 封装浮点型数据成为BigDecimal对象
  • 调用BigDecimal的方法,封装浮点型数据成为大数据对象
  •      public static BigDecimal valueOf(double val)
    
  •     BigDecimal a1 = BigDecimal.valueOf(a);
    
  •     BigDecimal b1 = BigDecimal.valueOf(b);
    

2.使用第二步 image.png

3.使用第三步:类型转回double

四.date

表达此时此刻的时间 使用方法:

  • 1.构造对象:Date d = new Date();
  • public long getTime() : 获取时间毫秒值:从1970-1-1 00:00:00开始走到此刻的总毫秒数:long time = d.getTime();
  • Date d2 = new Date(time): 把时间毫秒值转换成日期对象。
  • public void setTime(long time): 设计日期对象为当前时间毫秒值对应的日期。

五.SimpleDateFormat

作用:格式化、解析时间的。 构建对象:public SimpleDateFormat(String pattern) 方法: 格式化日期对象:public String format(Date d) 格式化时间毫秒值public String format(Object o) 把字符串时间解析成日期对象:public Date parse(String date):

六.日历Calendar

作用:代表日历,获取信息更丰富 构造对象:

    1、得到日历对象
    Calendar c = Calendar.getInstance();
    System.out.println(c);

     2、获取信息:public int get(int field):
    int year = c.get(Calendar.YEAR);
    System.out.println(year);

    int days = c.get(Calendar.DAY_OF_YEAR);
    System.out.println(days);

    3、获取日期对象(了解)
    Date d = c.getTime();
    System.out.println(d);

    4时间毫秒值(了解)
    long time = c.getTimeInMillis();
    System.out.println(time);

    5、修改日历的时间(需求:问89天后是什么日子)
    参数一:信息字段:一年中的第几天
    参数二:往后加多少天。
    c.add(Calendar.DAY_OF_YEAR, 89);
    Date d1 = c.getTime();
    System.out.println(d1);

    c.set(Calendar.DAY_OF_YEAR, 364); 直接修改日历到某一天!
    Date d2 = c.getTime();
    System.out.println(d2);
    

七.正则表达式

  • 作用:校验数据的格式的合法性。
  • 校验方法:String提供的:public boolean matches(String regex): 判断字符串与正则表达式是否匹配成功。
  • 校验规则:

image.png

正则做爬虫!

public static void main(String[] args) { 目标:拓展一下(如果去一段内容中爬取我们想要的信息(爬虫思想)) 1、记住一段需要爬取的内容。 String rs = "来黑马程序学习Java,电话020-43422424,或者联系邮箱" + "itcast@itcast.cn,电话18762832633,0203232323" + "邮箱bozai@itcast.cn,400-100-3233 ,4001003232";

    2、开始爬取,邮箱,电话号码,手机号。
    3、指定爬取的规则:爬取什么东西? 指定正则表达式,交给一个匹配规则对象
    Pattern p =
            Pattern.compile("()};

    4、把匹配规则对象与需要爬取的内容结合起来,得到一个匹配器对象。
    Matcher m = p.matcher(rs);

    5、开启使用匹配器对象爬取信息。
    while (m.find()){
        6、提取出来
        String data = m.group();
        System.out.println(data);
    }
}