java笔记_常用类

字符串

拼接

  • "+"运算符两边有字符串则会拼接
"a"+1 // "a1"
1+"a" // "1a"
  • concat拼接方法

比较

package com.atguigu.Common_classes;

public class demo01_string {
    public static void main(String[] args) {
        String s1 = "a";
        String s2 = "A";

        //相等
        System.out.println(s1.equals(s2));
        //忽略大小写的相等
        System.out.println(s1.equalsIgnoreCase(s2));

        // res > 0, a > b
        // res < 0, a < b
        // res == 0, a == b
        int res = s1.compareTo(s2);
        int Ignres = s1.compareToIgnoreCase(s2);
        System.out.println(res);
        System.out.println(Ignres);

        byte b1 = (byte) 'a';
        byte b2 = (byte) 'A';
        System.out.println("a = " + b1);
        System.out.println("A = " + b2);
    }
}
输出:
/*
false
true
32
0
a = 97
A = 65
 */

截断

package com.atguigu.Common_classes;

public class demo02 {
    public static void main(String[] args) {
        String temp = "Hello World";

        // 截断
        System.out.println(temp.substring(6, temp.length()));
        System.out.println(temp.substring(6));

        // 分隔
        String[] temps = temp.split(" ");
        System.out.println(temps.length);
        for (int i = 0; i < temps.length; i++) {
            System.out.println(temps[i]);
        }

        // 去除首尾空格
        String s = "  Hello World  ";
        System.out.println("!" + s.trim() + "!");
    }
}
/*
World
World
2
Hello
World
!Hello World!
 */

替换

package com.atguigu.Common_classes;

public class demo03 {
    public static void main(String[] args) {
        String temp1 = "Hello World";
        System.out.println(temp1.replace("World", "cen"));
        String temp2 = "Hello World1 World2";
        System.out.println(temp2.replaceAll("World1|World2", "cen"));
    }
}
/*
Hello cen
Hello cen cen
 */

大小写

package com.atguigu.Common_classes;

public class demo04 {
    public static void main(String[] args) {
        String temp = "Hello World";
        System.out.println(temp.toLowerCase());
        System.out.println(temp.toUpperCase());
    }
}
/*
hello world
HELLO WORLD
 */

查找

package com.atguigu.Common_classes;

public class demo05 {
    public static void main(String[] args) {
        String temp1 = "World Hello World";

        // 第一次出现的
        System.out.println(temp1.indexOf("World"));
        // 从给定的下标开始
        System.out.println(temp1.indexOf("World", 12));
        // 最后一个
        System.out.println(temp1.lastIndexOf("World"));
        // 是否包含
        System.out.println(temp1.contains("World"));
        // 是否以给定字符串开头
        System.out.println(temp1.startsWith("World"));
        // 是否以给定字符串结尾
        System.out.println(temp1.endsWith("World"));
        // 字符串是否为空
        System.out.println(temp1.isEmpty());
    }
}
/*
0
12
12
true
true
true
false
 */

StringBuilder

package com.atguigu.Common_classes;

public class demo06 {
    public static void main(String[] args) {
        StringBuilder temp = new StringBuilder();
        
        System.out.println(temp.append("abc"));
        // 反转
        System.out.println(temp.reverse());
        // 插入
        System.out.println(temp.insert(1, "d"));
    }
}
/*
abc
cba
cdba
 */

包装类

package com.atguigu.Common_classes.DataType;

public class demo01 {
    public static void main(String[] args) {
        Byte b = null;
        Short s = null;
        Integer i = null;
        Long l = null;
        Float f = null;
        Double d = null;
        Character c = null;
        Boolean bln = null;

        int temp = 10;
        //Integer i1 = new Integer(i);
        // 自动装箱
        //Integer i1 = Integer.valueOf(i);
        Integer i1 = temp;

        // 自动拆箱
        //int i2 = i1.intValue();
        int i2 = i1;
    }
}

日期类

package com.atguigu.Common_classes.Date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class demo01 {
    public static void main(String[] args) {

        // 时间戳(毫秒)
        System.out.println(System.currentTimeMillis());

        Date temp = new Date();
        System.out.println(temp);

        // Java格式化日期
        // y(Y). 年. yyyy. Y:每年有2周
        // m(M). MM:月份. mm:分钟.
        // d(D). dd:一个月中的日期. D:一年中的日期.
        // h(H). hh:12进制. HH:24进制.
        // s(S). s:秒. S:毫秒.

        // Date -> String
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
        String dateFormatString = sdf.format(temp);
        System.out.println(dateFormatString);

        // String -> Date
        String dateString = "2022-06-01 00:00:00.000";
        Date parseDate = null;
        try {
            parseDate = sdf.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        System.out.println(parseDate);

        // 根据时间戳构建指定的日期对象
        //temp.setTime(System.currentTimeMillis());
        // 获取时间戳
        //d.getTime();

        System.out.println(parseDate.before(temp));
        System.out.println(parseDate.after(temp));
    }
}
/*
1675008341470
Mon Jan 30 00:05:41 CST 2023
2023-01-30 00:05:41.041
Wed Jun 01 00:00:00 CST 2022
true
false
 */

日历类

package com.atguigu.Common_classes.Calender;

import java.util.Calendar;
import java.util.Date;

public class demo01 {
    public static void main(String[] args) {

        Calendar instance = Calendar.getInstance();
        //System.out.println(instance);

        System.out.println(instance.get(Calendar.YEAR));
        // 月份从0开始
        System.out.println(instance.get(Calendar.MONTH));
        System.out.println(instance.get(Calendar.DATE));

        instance.setTime(new Date());
        instance.add(Calendar.YEAR, -1);
    }
}
/*
2023
0
30
 */

打印日历

package com.atguigu.Common_classes.Calender;

import java.util.Calendar;

public class demo02 {
    public static void main(String[] args) {
        System.out.println("周一\t周二\t周三\t周四\t周五\t周六\t周日");

        Calendar now = Calendar.getInstance();
        // 获取这个月的最大天数
        int maxDay = now.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 将时间设置为这个月的第一天
        now.set(Calendar.DAY_OF_MONTH, 1);
        for (int i = 1; i <= maxDay; i++) {
            int weekX = now.get(Calendar.DAY_OF_WEEK);
            if(i == 1){
                if(weekX == Calendar.SUNDAY){
                    for (int j = 0; j < 6; j++){
                        System.out.print("\t");
                    }
                }else{
                    for (int j = 0; j < weekX-2; j++) {
                        System.out.print("\t");
                    }
                }
            }
            System.out.print(i);
            if(weekX == Calendar.SUNDAY){
                System.out.println();
            }else{
                System.out.print("\t");
            }
            now.add(Calendar.DATE, 1);
        }
    }
}

工具类

package com.atguigu.Common_classes.Util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class demo01 {
    public static void main(String[] args) {
        System.out.println(StringUtil.isEmpty(null));
        System.out.println(StringUtil.isEmpty(""));
        System.out.println(StringUtil.isEmpty("   "));
        System.out.println(StringUtil.isEmpty("abc"));

        System.out.println(StringUtil.makeString());
        System.out.println(StringUtil.makeString("abcdef", 6));

        System.out.println(StringUtil.formatDate(new Date(), "yyyy-MM-dd"));
        try {
            System.out.println(StringUtil.parseDate("2023-02-02", "yyyy-MM-dd"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

class StringUtil{

    // 非空判断
    public static boolean isEmpty(String str)
    {
//        // 如果字符串为null, 为空
//        if(str == null){
//            return true;
//        }
//        // 如果字符串为空字符串, 为空
//        if("".equals(str)){
//            return true;
//        }
//        // 如果字符串全是空格, 为空
//        if("".equals(str.trim())){
//            return true;
//        }
        if(str == null || "".equals(str.trim())){
            return true;
        }
        return false;
    }
    public static boolean isNotEmpty(String str)
    {
        return !isEmpty(str);
    }

    //生成随机字符串
    public static String makeString()
    {
        return UUID.randomUUID().toString();
    }
    public static String makeString(String from, int len)
    {
        if(len < 1){
            return "";
        }else{
            char[] chars = from.toCharArray();
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < len; i++) {
                Random random = new Random();
                int j = random.nextInt(chars.length);
                char c = chars[j];
                str.append(c);
            }
            return str.toString();
        }
    }

    // 转换字符串 : ISO8859-1 => str => UTF-8
    public static  String transfrom(String source, String encodeFrom, String encodeTo) throws Exception
    {
        byte[] bytes = source.getBytes(encodeFrom);
        return new String(bytes, encodeTo);
    }
    public static Date parseDate(String dateString, String format) throws Exception
    {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateString);
    }
    public static String formatDate(Date date, String format)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
}
/*
true
true
true
false
1025c98e-f0e3-4aaf-8c74-b3c0e4f4dfa4
edadab
2023-02-02
Thu Feb 02 00:00:00 CST 2023
 */

比较

package com.atguigu.Common_classes.compare;

public class demo01 {
    public static void main(String[] args) {

        // 基本数据类型, 双等号比较数值
        int num1 = 10;
        int num2 = 10;
        System.out.println("p1 " + (num1 == num2));
        double num3 = 10.0;
        System.out.println("p2 " + (num1 == num3));

        // 引用数据类型, 双等号比较变量的内存地址
        String s1 = "abc";
        String s2 = "abc";
        String s3 = new String("abc");
        System.out.println("p3 " + (s1 == s2));// 字符串常量池
        System.out.println("p4 " + (s1 == s3));
        System.out.println("p5 " + (s1.equals(s3)));

        Temp temp1 = new Temp();
        Temp temp2 = new Temp();
        System.out.println("p6 " + (temp1 == temp2));
        System.out.println("p7 " + (temp1.equals(temp2)));

        // 包装类型
        // Integer缓存: -128~127
        Integer i1 = Integer.valueOf(128);
        Integer i2 = Integer.valueOf(128);
        System.out.println("p8 " + (i1 == i2));
        System.out.println("p9 " + (i1.equals(i2)));
    }
}
class Temp{
    @Override
    public int hashCode() {
        return 1;
    }

    @Override
    public boolean equals(Object obj) {
        return true;
    }
}
/*
p1 true
p2 true
p3 true
p4 false
p5 true
p6 false
p7 true
p8 false
p9 true
 */

参考

尚硅谷. 【尚硅谷】7天搞定Java基础,Java零基础极速入门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y_cen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值