###String 常用方法:

```java
import java.util.Arrays;

public class Test2 {
    public static void main(String[] args) {
        char[] array = {'J','a','v','a',',','H','e','l','l','o',',','W','o','r','l','d'};
        String str = new String(array);
        System.out.println(str);
        System.out.println("str 的长度:"+str.length());
        System.out.println("str 是否为空:"+str.isEmpty());
        System.out.println("str 下标为2的字符:"+str.charAt(2));
        System.out.println("H 的下标是:"+str.indexOf('H'));
        String str2 = "Hello";
        System.out.println("str和str2是否相等:"+str.equals(str2));
        String str3 = "HELLO";
        System.out.println(str2.equals(str3));
        System.out.println(str2.equalsIgnoreCase(str3));
        System.out.println("str是否以Java开头:"+str.startsWith("Java"));
        System.out.println("str是否以Java结尾:"+str.endsWith("Java"));
        System.out.println("从2开始截取str:"+str.substring(2));
        System.out.println("从2到6截取str:"+str.substring(2, 6));
        System.out.println(str.replaceAll("World", "Java"));
        String[] array2 = str.split(",");
        for(int i = 0; i < array2.length;i++) {
            System.out.println(array2[i]);
        }
        System.out.println(Arrays.toString(str.toCharArray()));
        System.out.println(str3.toLowerCase());
        System.out.println(str2.toUpperCase());
    }
}

```



### StringBuffer

与String类似,StringBuffer底层也是用一个数组来存储字符串的值,并且数组的默认长度为16,即一个空的StringBuffer对象,数组长度为16。

当我们调用有参构造创建一个StringBuffer对象时,数组长度就不是16了,而是根据当前对象的值来决定数组的长度,"值的长度+16"作为数组的新长度。

一个StringBuffer创建完成之后,有16字节的空间可以对其值进行修改,如果修改的值范围超过了16字节,则调用相关方法对底层数组进行扩容,并且保持引用不变。

StringBuffer常用方法:

public StringBuffer()                                            无参构造,创建一个空的StringBuffer

public StringBuffer(String str)                                    有参构造

public synchronized int length()                                     返回StringBuffer的长度

public synchronized char charAt(int index)                         返回字符串中指定位置的字符

public synchronized StringBuffer append(String str)                 追加字符

public synchronized StringBuffer delete(int start,int end)            删除指定区间的字符

public synchronized StringBuffer deleteCharAt(int index)            删除指定位置的字符

public synchronized StringBuffer replace(int start,int end,String str)    将指定区间内的值全部替换为str

public synchronized String substring(int start)                        截取字符串从指定位置开始到结尾

public synchronized String substring(int start,int end)                截取字符串从指定位置开始到指定位置结束

public synchronized StringBuffer insert(int offset,String str)            向指定位置插入str

public int indexOf(String str)                                        从头开始查找字符串的位置

public int indexOf(String str,int fromIndex)                            从指定位置开始查找字符串的位置

public synchronized StringBuffer reverse()                            进行反转

public synchronized String toString()                                返回StringBuffer对应的String



```java
public class Test3 {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer();
        System.out.println(stringBuffer.length());
        stringBuffer = new StringBuffer("Hello World");
        System.out.println(stringBuffer);
        System.out.println("下标为2的字符是:"+stringBuffer.charAt(2));
        stringBuffer = stringBuffer.append("Java");
        System.out.println(stringBuffer);
        stringBuffer = stringBuffer.delete(3, 6);
        System.out.println(stringBuffer);
        stringBuffer = stringBuffer.deleteCharAt(3);
        System.out.println(stringBuffer);
        stringBuffer = stringBuffer.replace(2, 3, "StringBuffer");
        System.out.println(stringBuffer);
        String str = stringBuffer.substring(2,8);
        System.out.println(str);
        stringBuffer = stringBuffer.insert(6, "six");
        System.out.println(stringBuffer);
        System.out.println(stringBuffer.indexOf("e"));
        System.out.println(stringBuffer.indexOf("e",6));
        stringBuffer = stringBuffer.reverse();
        System.out.println(stringBuffer);
        System.out.println(stringBuffer.toString());
    }
}
```



### 日期类

java.util.Data 和 java.util.Calendar

- Date

使用 java.text.SimpleDateFormate 类对 Date 对象进行格式化处理,SimpleDateFormat 提供了模版标记。

y                            年,yyyy表示4位数的年份信息

M                            月,MM表示2位数的月份信息

m                            分钟,mm表示2位数的分钟信息

d                            天,dd表示2位数的天信息

H                            小时,HH表示2位数的24小时制下的小时信息

h                            小时,hh表示2位数的12小时制下的小时信息

s                            秒,ss表示2位数的秒信息

S                            毫秒,SSS表示3位数的毫秒信息



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

public class Test4 {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String str = simpleDateFormat.format(date);
        System.out.println(str);
    }
}
```



### Calendar

常用的用来存储日期数据的常量:

public static final int YEAR                                年

public static final int MONTH                            月

public static final int DAY_OF_MONTH                    天,以月为单位,即当天是该月中的第几天

public static final int DAY_OF_YEAR                        天,以年为单位,即当天是该年中的第几天

public static final int HOUR_OF_DAY                        小时

public static final int MINUTE                            分钟

public static final int SECOND                            秒

public static final int MILLSECOND                        毫秒

常用的方法:

public static Calendar getInstance()                    获取系统对应的 Calendar 实例化对象

public void set(int field,int value)                        给静态常量赋值

public int get(int field)                                获取静态常量

public final Date getTime()                            获取Calendar对应的Date对象



```java
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Test5 {
    public static void main(String[] args) {
        //计算2019年8月1号所在的周是2019年的第几周
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2019);
        calendar.set(Calendar.MONTH,7);
        calendar.set(Calendar.DAY_OF_MONTH,28);
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        System.out.println(week);
        //计算2019年8月1号后21天的日期
        calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+21);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String str = simpleDateFormat.format(calendar.getTime());
        System.out.println(str);
    }
}
```

Test.java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    //初始化用户集合
    private static List<User> userList;
    static {
        userList = new ArrayList<>();
        userList.add(new User("张三",22,"正常"));
        userList.add(new User("李四",23,"正常"));
        userList.add(new User("王五",24,"正常"));
        userList.add(new User("小明",25,"正常"));
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = 0;
        do {
            System.out.println("欢迎使用用户管理系统!");
            System.out.println("1.查询用户");
            System.out.println("2.添加用户");
            System.out.println("3.删除用户");
            System.out.println("4.账号冻结");
            System.out.println("5.账号解冻");
            System.out.println("6.退出系统");
            System.out.print("请选择:");
            num = scanner.nextInt();
            switch (num) {
                case 1:
                    System.out.println("------查询用户------");
                    System.out.println("编号\t\t名称\t\t年龄\t\t状态");
                    for(int i=0;i<userList.size();i++) {
                        User user = userList.get(i);
                        System.out.println((i+1)+"\t\t"+user.getName()+"\t\t"+user.getAge()+"\t\t"+user.getState());
                    }
                    System.out.print("输入0返回上层菜单:");
                    num = scanner.nextInt();
                break;
                case 2:
                    System.out.println("------添加用户------");
                    System.out.print("请输入用户名称:");
                    String name = scanner.next();
                    boolean flag = false;
                    //判断该用户是否存在
                    for(int i = 0;i<userList.size();i++) {
                        User user = userList.get(i);
                        if(user.getName().equals(name)) {
                            System.out.println("该用户已存在!");
                            flag = true;
                            break;
                        }
                    }
                    if(!flag) {
                        System.out.print("请输入用户年龄:");
                        int age = scanner.nextInt();
                        userList.add(new User(name,age,"正常"));
                        System.out.println(name+"已添加成功!");
                    }
                    System.out.print("输入0返回上层菜单:");
                    num = scanner.nextInt();
                break;
                case 3:
                    System.out.println("------删除用户------");
                    System.out.print("请输入用户名称:");
                    name = scanner.next();
                    //判断该用户是否存在
                    flag = false;
                    for(int i = 0; i < userList.size();i++) {
                        User user = userList.get(i);
                        if(user.getName().equals(name)) {
                            flag = true;
                            userList.remove(user);
                            System.out.println("删除成功!");
                            break;
                        }
                    }
                    if(!flag) {
                        System.out.println(name+"不存在!操作失败");
                    }
                    System.out.print("输入0返回上层菜单:");
                    num = scanner.nextInt();
                break;
                case 4:
                    System.out.println("------账号冻结------");
                    System.out.print("请输入用户名称:");
                    name = scanner.next();
                    //判断用户是否存在
                    flag = false;
                    for(int i = 0; i < userList.size();i++) {
                        User user = userList.get(i);
                        if(user.getName().equals(name)) {
                            flag = true;
                            if(user.getState().equals("冻结")) {
                                System.out.println(name+"已冻结!");
                            }else {
                                user.setState("冻结");
                                System.out.println(name+"冻结成功");
                            }
                            break;
                        }
                    }
                    if(!flag) {
                        System.out.println(name+"不存在!操作失败");
                    }
                    System.out.print("输入0返回上层菜单:");
                    num = scanner.nextInt();
                break;
                case 5:
                    System.out.println("------账号解冻------");
                    System.out.print("请输入用户名称:");
                    name = scanner.next();
                    //判断用户是否存在
                    flag = false;
                    for(int i = 0; i < userList.size();i++) {
                        User user = userList.get(i);
                        if(user.getName().equals(name)) {
                            flag = true;
                            if(user.getState().equals("正常")) {
                                System.out.println(name+"状态正常");
                            }else {
                                user.setState("正常");
                                System.out.println(name+"解冻成功!");
                            }
                            break;
                        }
                    }
                    if(!flag) {
                        System.out.println(name+"不存在!操作失败");
                    }
                    System.out.print("输入0返回上层菜单:");
                    num = scanner.nextInt();
                break;
                case 6:
                    System.out.println("感谢使用用户管理系统!");
                    return;
            }
        }while(num == 0);
    }
}

 

Test2.java

import java.util.Arrays;

public class Test2 {
    public static void main(String[] args) {
        char[] array = {'J','a','v','a',',','H','e','l','l','o',',','W','o','r','l','d'};
        String str = new String(array);
        System.out.println(str);
        System.out.println("str 的长度:"+str.length());
        System.out.println("str 是否为空:"+str.isEmpty());
        System.out.println("str 下标为2的字符:"+str.charAt(2));
        System.out.println("H 的下标是:"+str.indexOf('H'));
        String str2 = "Hello";
        System.out.println("str和str2是否相等:"+str.equals(str2));
        String str3 = "HELLO";
        System.out.println(str2.equals(str3));
        System.out.println(str2.equalsIgnoreCase(str3));
        System.out.println("str是否以Java开头:"+str.startsWith("Java"));
        System.out.println("str是否以Java结尾:"+str.endsWith("Java"));
        System.out.println("从2开始截取str:"+str.substring(2));
        System.out.println("从2到6截取str:"+str.substring(2, 6));
        System.out.println(str.replaceAll("World", "Java"));
        String[] array2 = str.split(",");
        for(int i = 0; i < array2.length;i++) {
            System.out.println(array2[i]);
        }
        System.out.println(Arrays.toString(str.toCharArray()));
        System.out.println(str3.toLowerCase());
        System.out.println(str2.toUpperCase());
    }
}

 

Test3.java

public class Test3 {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer();
        System.out.println(stringBuffer.length());
        stringBuffer = new StringBuffer("Hello World");
        System.out.println(stringBuffer);
        System.out.println("下标为2的字符是:"+stringBuffer.charAt(2));
        stringBuffer = stringBuffer.append("Java");
        System.out.println(stringBuffer);
        stringBuffer = stringBuffer.delete(3, 6);
        System.out.println(stringBuffer);
        stringBuffer = stringBuffer.deleteCharAt(3);
        System.out.println(stringBuffer);
        stringBuffer = stringBuffer.replace(2, 3, "StringBuffer");
        System.out.println(stringBuffer);
        String str = stringBuffer.substring(2,8);
        System.out.println(str);
        stringBuffer = stringBuffer.insert(6, "six");
        System.out.println(stringBuffer);
        System.out.println(stringBuffer.indexOf("e"));
        System.out.println(stringBuffer.indexOf("e",6));
        stringBuffer = stringBuffer.reverse();
        System.out.println(stringBuffer);
        System.out.println(stringBuffer.toString());
    }
}

 

Test4.java

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

public class Test4 {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String str = simpleDateFormat.format(date);
        System.out.println(str);
    }
}

 

Test5.java

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Test5 {
    public static void main(String[] args) {
        //计算2019年8月1号所在的周是2019年的第几周
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2019);
        calendar.set(Calendar.MONTH,7);
        calendar.set(Calendar.DAY_OF_MONTH,28);
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        System.out.println(week);
        //计算2019年8月1号后21天的日期
        calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+21);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String str = simpleDateFormat.format(calendar.getTime());
        System.out.println(str);
    }
}

 

User.java

public class User {
    private String name;
    private int age;
    private String state;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public User(String name,int age,String state) {
        this.name = name;
        this.age = age;
        this.state = state;
    }
}

 

posted on 2019-07-11 22:16  HiJackykun  阅读(367)  评论(0编辑  收藏  举报