15.java常用工具类

15.java常用工具类,第1张

一、System类
  1. 构造方法私有化,包含了一些类字段和方法,有标准的输入流和输出流与错误信息输出流
  2. 常量
    • static final InputStream in :标准的输入流(默认关联键盘)
    • static final PrintStream out:标准的输出流(默认关联控制台)
    • static final PrintStream err:标准的默认错误输出流
  3. 常用方法
    • static void exit(int status):终止jvm,非0状态表示异常终止
    • static long currentTimeMillis():获取时间戳
    • static void gc():垃圾回收
  4. 代码
package com.la.demo;
import java.util.Scanner;
public class TestSystem {
    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        System.out.println(l);
        System.out.println("我将众神埋葬");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入");
        System.out.println(scanner.next());
        System.gc();
        System.err.println("错误");
        System.exit(0);
    }
}
二、Date类
  1. 时间类,精确到毫秒
  2. 构造方法
    • Date() :构造获取当前的时间
    • Date(long time):构造一个从1970,01,01,00,00,00+time的时间戳的日期
  3. 常用方法
    • void setTime(long time):设置date对象,表示1970,01,01,00,00,00以后的time时间,等同有参构造
    • long getTime():或Date对象的时间戳
  4. 案例
package com.la.demo;
import java.util.Date;
public class TestDate {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);
        Date date1 = new Date(1000);
        System.out.println(date1);
        long time1 = date.getTime();
        System.out.println(time1);
        date.setTime(1000);
        long time = date.getTime();
        System.out.println(time);
    }
三、Calendar类
  1. 是一个抽象类,提供了诸如:YEAR/MONTH/DAY_OF_MONTH/HOUR等字段
  2. 常量
    • static final int YEAR
    • static final int MONTH +1
    • static final int DAY_OF_MONTH
    • static final int HOUR
    • static final int MINUTE
    • static final int SECOND
    • static final int DAY_OF-WEEK -1
    • static final int WEEK_OF_MONTH
  3. 常用方法
    • static Calender getInstance() :返回一个calender实例
    • int get(int field):获取指定日期格式的值
    • void set(int field,int value) :给指定日期格式赋值
    • void add(int filed,int amount):给指定日期格式加时间量
    • final Date getTime():calender转为date类型
    • final void setTime(Date date):把date类型转为calender类型
  4. 案例
package com.la.demo;
import java.util.Calendar;
import java.util.Date;
public class TestCalender {
    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        System.out.println(instance);
        System.out.println(instance.get(Calendar.YEAR));
        System.out.println(instance.get(Calendar.MONTH )+1);
        System.out.println(instance.get(Calendar.DATE));
        System.out.println(instance.get(Calendar.HOUR));
        System.out.println(instance.get(Calendar.MINUTE));
        System.out.println(instance.get(Calendar.SECOND));
        System.out.println(instance.get(Calendar.MILLISECOND));
        instance.add(Calendar.HOUR,-2);
        System.out.println(instance);
        instance.set(Calendar.MINUTE,50);
        System.out.println(instance);
        Date time = instance.getTime();
        System.out.println(time);
        instance.setTime(time);
    }
}
四、SimpleDateFormat类
  1. 格式化日期类
  2. 构造方法
    • simpleDateFormat(String pattern)
  3. 常用方法
    • StingBuffer format(Date date) :将日期转换为格式化的字符串
    • Date parse(String text):将字符串转换为Date日期类型
  4. 案例
package com.la.demo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws ParseException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的生日:");
        String next = scanner.next();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(next);
        long time = parse.getTime();
        long time1 = System.currentTimeMillis();
        long ok=time1-time;
        Date date = new Date(ok);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
        System.out.println(ok/1000/60/60/24);
    }
}
五、Scanner类
  1. 扫描器
  2. 构造方法
    • Scanner (File source)
    • Scanner(InputStream source)
    • Scanner(String source)
  3. 常用方法
    • String next()
    • String nextLine()
    • boolean hasNextXxx() :判断键盘录入与接收的数据类型是否匹配
  4. 案例
package com.la.demo;
import java.util.Scanner;
public class TestScaner {
    public static void main(String[] args) {
        String str2="";
        Scanner scanner = new Scanner(System.in);
//        System.out.println("接受一串不含空格的字符串:");
//        String next = scanner.next();
//        System.out.println(next);
        //问题:nextInt,与nextLine不能同时存在,因为在接受int时,按回车,回车会被nextLine接收
        //解决:实例化两个Scanner对象
        //解决:将nextLine改为next
        String str1 = scanner.nextLine();
        System.out.println(Integer.parseInt(str1));

        if (scanner.hasNext()){
             str2 = scanner.next();
        }
        System.out.println(str2);
    }
}
六、Arrays类
  1. 数组工具类,构造方法私有,不能实例化
  2. 常用方法
    • static int binarySearch(byte[] a,byte key) :查找指定元素在数组中的索引位置(数组必须有序)
    • static int[] copyOf(int[] original,int newLength):指定长度复制一个新的数组
    • static String toString(int[] a):打印
    • static void sort(int[] a) :排序
    • static void fill(int[] a,int val):把数组a的值全改为val
  3. 案例
package com.la.demo;
import java.util.Arrays;
public class TestArrays {
    public static void main(String[] args) {
        int[] a={1,2,3,5,6,4,1,9};
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.binarySearch(a, 9));
        int[] ints = Arrays.copyOf(a, 10);
        System.out.println(Arrays.toString(ints));
        Arrays.fill(ints,100);
        System.out.println(Arrays.toString(ints));

    }
}

七、BigInteger/Bigdecimal类
  1. 大数据类型
  2. 构造方法
    • BigInteger(String val) :将String类型整数封装成一个bigInteger类型
  3. 常用方法
    • BigInteger abs() :绝对值
    • BigInteger add(BigInteger val) : 加法
    • BigInteger substract(BigInteger val):减法
    • BigInteger multiply(BigInteger val):乘法
    • BigInteger divide(BigInteger val):除法
    • BigInteger remainder(BigInteger val):取余
    • BigInteger negate() :取反
  4. 案例
package com.la.demo;
import java.math.BigInteger;
public class TestBigInteger {
    public static void main(String[] args) {
        String s="158368964388";
        String s1="-22";
        BigInteger bigInteger = new BigInteger(s);
        BigInteger bigInteger2 = new BigInteger(s1);
        System.out.println(bigInteger2.abs());
        System.out.println(bigInteger.negate());
        System.out.println(bigInteger.add(bigInteger2));
        System.out.println(bigInteger.subtract(bigInteger2));
        System.out.println(bigInteger.multiply(bigInteger2));
        System.out.println(bigInteger.divide(bigInteger2));
        System.out.println(bigInteger.remainder(bigInteger2));
    }
}
package com.la.demo;
import java.math.BigDecimal;
public class TestDecimal {
    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("5635533");
        BigDecimal bigDecimal1 = new BigDecimal("36621");
        System.out.println(bigDecimal.divide(bigDecimal1, BigDecimal.ROUND_UP));
      //  System.out.println(bigDecimal.divide(bigDecimal1));
        System.out.println(bigDecimal.divide(bigDecimal1,3,BigDecimal.ROUND_UP));
    }
}

八、作业
package com.la.demo2;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Work1 {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse("2020-2-14");
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        int i=instance.get(Calendar.DAY_OF_WEEK)-1;
        if (i==0){
            i=7;
        }
        System.out.println(i);
        System.out.println(instance.get(Calendar.WEEK_OF_MONTH));
    }
}
package com.la.demo2;

import java.util.Date;

public class Work2 {
    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        String str="";
        for (int i = 0; i < 100000; i++) {
            str+=i;
        }
        long l1 = System.currentTimeMillis();
        long sub=l1-l;
        System.out.println(sub);

        Date date = new Date();
        StringBuilder stringBuilder = new StringBuilder();
        long time = date.getTime();
        for (int i = 0; i < 100000; i++) {
            stringBuilder.append(i);
        }
        long time1 = new Date().getTime();
        long sub1=time1-time;
        System.out.println(sub1);

    }
}
package com.la.demo2;
public class Work3 {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        String[] str=new String[6];
        int[] a={7,8,9};
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j <a.length; j++) {
                for (int k = 0; k < a.length; k++) {
                    if (a[k]!=a[i]&&a[k]!=a[j]&&a[i]!=a[j]){
                        stringBuilder.append(a[k]).append(a[j]).append(a[i]).append(" ");
                    }
                }
            }

        }
        System.out.println(stringBuilder);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/787318.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存