关于java的API的部分使用

API的语法使用

object

object是所有子类中的父类,所以父类toString的意义是为了让子类重写以便返回内容

toString:不适用将返回伪地址(字符内存,个人习惯叫伪地址),使用返回内容

equals:是默认用来比较两个对象“地址”是否一致,一致为true反之flase!主要为了方便子类可以自己指定相等的比较规则。

对比的是地址。

*objects类的常用方法

objects的equlas方法更加可靠,对object进行了升级可以避免空指针异常还嫩刚给正常结果

多了一个空指针校验,必须不是空才会比较

*包装类的应用场景

语法Interger:(万物皆为对象但是变量不是对象)起初是让字符串拼接(用处不大)

更新后:最有用的用法是将字符串转换为基本类型变量。 

通常使用Interger.valueof或Interger.passInt  看使用变量类型

    public static void main(String[] args) {
        String s1 = "99";
        int t1 = Integer.valueOf(s1);
        System.out.println(t1+1);

        String s2 = "30.8";
//        double t2 = Double.parseDouble(s2);
        Double t2 = Double.valueOf(s2);
        System.out.println(t2 + 0.5);
    }

//输出为100

*StringBuilder与StringBuffer

StringBuilder是内容可变的字符串,拼接字符串性能号,代码更优雅

字符串拼接StringBuilder定义    append(“字符串)

反转内容

sb.reverse();

获取长度:sb.length()

总之:StringBuilder只是一种操作字符串的手段。 String才是目的。

StringBuilder性能比String好 ,长字符串输出是很快

    public static void main(String[] args) {
        // 目标:学会使用StringBuilder: 操作字符串数据的。
        StringBuilder sb = new StringBuilder();
        sb.append("笨蛋").append(666).append(true).append(99.5);
        System.out.println(sb);
    }

StringBuilder可以进行拼接对字符串定义与拼接使用,对数组进行改变动态

    public static void main(String[] args) {
        // 目标:掌握StringBuilder的应用。
        int[] arr = {11,22,33,44};
        String re = getArrayData(arr);
        System.out.println(re);
    }

    private static String getArrayData(int[] arr) {
        if (arr == null){
            return null;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]).append(i == arr.length-1 ? "":", ");
        }
        sb.append("]");

        return sb.toString();
    }

StringJoiner:

lJDK8出现的一个可变的、操作字符串的容器。

使用它拼接字符串的时候,不仅高效,而且代码的编写更加的方便

 StringJoiner sj = new StringJoiner(",","[","]");引用格式

    public static void main(String[] args) {
        int[] arr = {11,22,33,44};
        String re = getArrAYdate(arr);
        System.out.println(re);
    }

    public static String getArrAYdate(int[] arr){
        if (arr == null){
            return null;
        }

        StringJoiner sj = new StringJoiner(",","[","]");
        for (int i = 0; i < arr.length; i++) {
            sj.add(arr[i]+"");
        }
        return sj.toString();
    }

System

System.exit():终止当前运行的Java虚拟机

Runtime

.availableProcessors()        // 获取虚拟机能够使用的处理器数。
.totalMemory()/1024.0/1024.0        //返回Java虚拟机中的内存总量。 字节数。
.freeMemory()/1024.0/1024.0         //回Java虚拟机中的可用内存量
.exec("软件启动路径路径")//或程序中编辑好的路径直接输入名如.exec("QQ");直接启动

也可以控制关闭与启动

        Scanner sc = new Scanner(System.in);
        System.out.println("是否关闭y/n");
        switch (sc.next()){
            case "y":
                p1.destroy(); // 关闭软件
                break;
            default:
                System.out.println("取消");
        }
    }

BigDecimal

:用于解决浮点型运算时,出现结果失真的问题。

BigDecimal不推荐使用double,推荐使用String因为能够解决精度问题不会多输出产生误差,

(就是将浮点型字符串转换成基础变量数字!!)

.add(  );.        //加

subtract(  );        //减

.multiply(  );.        //乘

divide(  );        //除

前面先定义.divide(除数 ,保留,位数,舍入模式);

最后输出.doubleValue();

    public static void main(String[] args) {
        double a = 0.5;
        double b = 0.8;
        BigDecimal a1 = BigDecimal.valueOf(a);
        BigDecimal b1 = BigDecimal.valueOf(b);

        BigDecimal c1 = a1.add(b1); //加
        System.out.println(c1);
        BigDecimal c2 = a1.subtract(b1);//减
        System.out.println(c2);
        BigDecimal c3 =a1.multiply(b1);//乘
        System.out.println(c3);
        BigDecimal c4 = a1.divide(b1);
        System.out.println(c4);

        // 3、创建BigDecimal对象 实现精度控制
        BigDecimal i = new BigDecimal("0.1");
        BigDecimal j = new BigDecimal("0.3");
        /**
         * 参数一:除数
         * 参数二:保留位数
         * 参数三:舍入模式
         */

        BigDecimal k = i.divide(j,2, RoundingMode.HALF_UP);
        System.out.println(k);
        // 4、BigDecimal是解决精度问题的手段。
        // double才是目的。
        double result = k.doubleValue();//0.33
        System.out.println(result);
掌握Instant的使用
    public static void main(String[] args) {
       // 1、创建Instant的对象,获取此刻时间信息。
        Instant now = Instant.now(); // 世界标准时间
        System.out.println(now);

        // 2、获取总秒数
        System.out.println(now.getEpochSecond());
        // 3、不够1秒的纳秒数
        System.out.println(now.getNano());

        // Instant对象的作用:做代码的性能分析,或者记录用户的操作时间点

    }
目标:掌握JDK 8新增的DateTimeFormatter格式化器的用法。(重点)
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        // 1、创建一个日期时间格式化器对象出来。参数:格式化的时间形式。
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd HH:mm:ss EEE a");

        // 2、对时间进行格式化
        String result = dtf.format(ldt);
        System.out.println(result);

        // 3、格式化时间,其实还有一种方案。
        String result2 = ldt.format(dtf);
        System.out.println(result2);

        // 4、解析时间:解析时间一般使用LocalDateTime提供的解析方法来解析。
        String dateStr = "2023-11-11 11:11:11";
        // 创建日期时间格式化对象 : 参数格式,必须与被解析的时间格式一样
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 调用LocalDateTime的parse方法,按照dtf2规定的格式解析字符串时间成为日期时间对象。
        LocalDateTime ldt2 = LocalDateTime.parse(dateStr, dtf2);
        System.out.println(ldt2);
        System.out.println(ldt2.getDayOfYear());
    }
 
掌握Period的作用:计算机两个日期相差的年数,月数、天数。
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2023, 8, 10);
        LocalDate end = LocalDate.now(); // 2023-11-11

        // 1、创建Period对象,封装两个日期对象。
        Period period = Period.between(start, end);

        // 2、通过period对象获取两个日期对象相差的信息。
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }

    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2023, 11, 11, 11, 10, 10);
        LocalDateTime end = LocalDateTime.of(2024, 1, 1, 11, 11, 11);

        // 1、得到Duration对象
        Duration duration = Duration.between(start, end);

        // 2、获取两个时间对象间隔的信息
        System.out.println(duration.toDays());// 间隔多少天
        System.out.println(duration.toHours());// 间隔多少小时
        System.out.println(duration.toMinutes());// 间隔多少分
        System.out.println(duration.toSeconds());// 间隔多少秒
        System.out.println(duration.toMillis());// 间隔多少毫秒
        System.out.println(duration.toNanos());// 间隔多少纳秒

        System.out.println("-------------------------高考倒计时----------------------------------");
        // 需求:高考时间是: 2024-06-07 09:00:00
        LocalDateTime ldt = LocalDateTime.now(); // 今天
        System.out.println(ldt);

        String str = "2024-06-07 09:00:00";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt2 = LocalDateTime.parse(str , dtf); // 高考时间

        // 计算现在离高考差多少天,多少小时,多少分,多少秒。
        Duration duration1 = Duration.between(ldt, ldt2);
        System.out.println(duration1.toDays() + " " + duration1.toHoursPart() + " " + duration1.toMinutesPart() + " " + duration1.toSecondsPart());
    }

关于日期的常用大概是这些,总结小经验分享

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值