JAVA 常用类的学习 - 8.30

java常用类

Object类

  • toString() :直接打印对象调用该方法,一般在类中重写(override)。

    默认的 toString() 是打印类全名+hashcode

  • hashCode():返回该对象的哈希码值,内存地址。

        public class Poker {
            private String follow;
            private String point;
            public String getFollow() {
                return follow;
            }
            public void setFollow(String follow) {
                this.follow = follow;
            }
            public String getPoint() {
                return point;
            }
            public void setPoint(String point) {
                this.point = point;
            }
    
    
    
            @Override
            public String toString() {
                return follow + point;
            }
            public Poker(String follow, String point) {
                super();
                this.follow = follow;
                this.point = point;
            }
            public static void main(String[] args) {
                Poker poker = new Poker("H","2");
                System.out.println(poker);
            }
        }
    

String类

构建字符串的方式
    String s1 = new String("hello");
    char[] cs = {'h','e','l','l','o'};
        String s2 = new String(cs);
        String s3 = "hello"; // 最常用
常用的方法
  • charAt
  • replace
  • split
  • indexOf
  • substring
  • contains
public class Demo1 {
    public static void main(String[] args) {
        // 字符串的构建方式

        String s1 = new String("hello");

        char[] cs = { 'h', 'e', 'l', 'l', 'o' };
        String s2 = new String(cs);

        String s3 = "hello"; // 最常用

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

        System.out.println(s3.charAt(1));

        String s4 = s3.concat(",world!");

        System.out.println(s4);

        System.out.println(s4.contains("world1"));

        String s5 = "d:/a/index.js";
        System.out.println(s5.endsWith(".js"));

        // 对于字符串的比较操作,一般的场景都是比较字符串的内容是否一致
        String s6 = "tom";
        String s7 = new String("tom");
        System.out.println(s6 == s7); // false
        System.out.println(s6.equals(s7)); // true

        String s8 = "hello,world!";
        System.out.println(s8.indexOf("world")); // 6

        String s9 = "a1a2a3b4a5";
        System.out.println(s9.indexOf("a")); // 0
        System.out.println(s9.indexOf("a", 1)); // 2

        List<Integer> indexs = getIndexsOfStr("1a22a3444a4b", 'a');
        for (Integer index : indexs) {
            System.out.print(index + "\t");
        }



        // 打印文件的后缀
        String s10 = "d:/a/index.js";
        int s10_1 = s10.lastIndexOf(".") + 1;
        String s10_2 = s10.substring(s10_1);
        System.out.println(s10_2);

        String s11 = "abcabcabc";
        System.out.println(s11.replace("a", "A"));
        System.out.println(s11.replaceFirst("a", "A"));

        String s12 = "a-b-c-d-e-f";
        String[] s12s = s12.split("-");
        for (String s : s12s) {
            System.out.println(s);
        }

        String s13 = "20171108001";
        System.out.println(s13.startsWith("20171108"));

        System.out.println(s12.toUpperCase());

        String s14 = " hello,wolrd!  ";
        String s15 = s14.trim();
        System.out.println("#"+s14+"#");
        System.out.println("#"+s15+"#");

    }

    // 传入一个字符串,输出某个字符在传入字符串中的所有位置

    public static List<Integer> getIndexsOfStr(String src, char c) {
        List<Integer> rs = new ArrayList<Integer>();

        if (null != src) {
            char[] cs = src.toCharArray();

            for (int i = 0; i < cs.length; i++) {
                if (cs[i] == c) {
                    rs.add(i);
                }
            }
        }

        return rs;
    }

    // 传入一个字符串,输出某个字符在传入字符串中的所有位置

    public static List<Integer> getIndexsOfStr(String src, String c) {
        List<Integer> rs = new ArrayList<Integer>();

        if (null != src) {
        }

        return rs;
    }

}

StringBuffer类

  • 在进行大数据的字符串拼接时使用 StringBuffer
public class Demo3 {
    public static void main(String[] args) {

        Date d11 = new Date();
        String s = "";
        for (int i = 0; i < 100000; i++) {
            s += 1;
        }
        Date d22 = new Date();
        System.out.println(d22.getTime() - d11.getTime()); // 3418

        Date d1 = new Date();
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < 100000; i++) {
            sb.append(i);
        }
        Date d2 = new Date();
        System.out.println(d2.getTime() - d1.getTime()); // 16
    }
}

日期 java.util.Date 和 Calendar

Date 和 SimpleDateFormat 的运用

public class Demo4 {
    public static void main(String[] args) throws ParseException {
        // 构建当前服务器的时间对象
        Date now = new Date();
        System.out.println(now);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        System.out.println(sdf.format(now)); // 格式化的目的是用于显示

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        String dateStr = "2017/10/09 13:04:30";
        Date now2 = sdf2.parse(dateStr); // 通过一个字符串解析成日期对象
        System.out.println(now2.getYear()+1900);


    }
}

Calendar类

Calendar 是更加先进的日期处理类

如果只是简单的对日期进行输入,用 Date 就可以了,但是如果要对日期进行复杂的运算,就要使用 Calendar 类,Calendar 和 Date 是可以互相转化的。

  • Calendar 和 Date 的转换
  • 使用 Calendar 进行复杂的日期运算
  • Calendar 只是个工具,具体的日期还是要使用 Date 对象
public class Demo5 {
    public static void main(String[] args) throws ParseException {
        String dateStr = "2017/11/11";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date date = sdf.parse(dateStr);

        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);

        rightNow.add(Calendar.DAY_OF_MONTH, 30);
        rightNow.add(Calendar.MONTH, 1);

        System.out.println(rightNow.get(Calendar.DAY_OF_MONTH));
        System.out.println(rightNow.get(Calendar.MONTH));

        rightNow.set(Calendar.YEAR, 2019);

        rightNow.add(Calendar.DAY_OF_MONTH, -1);

        System.out.println(sdf.format(rightNow.getTime()));

    }
}

猜你喜欢

转载自blog.csdn.net/ShanGe9527/article/details/82227519