天天看点

JAVA常用类(String,StringBuffer,日期类)

String类

/**
* String : 是内容不可改变的Unicode字符序列(CharSequence).
* 可以简单地理解为是一个char[]
*                  0 2       10       15       21        27   32 34
* String string = "  abcAQQY 我喜欢你,你喜欢我吗?我不喜欢你 zzz123  ";
*
* ###public int length(). string.length() => 35 获取字符串长度(字符数)
* ###public char charAt(int index) 获取指定下标位置处的字符 string.charAt(12) => '欢', string.charAt(18) => '我';
* ##public char[] toCharArray() 获取内部的char[]
*       char result[] = new char[value.length]; // 创建一个新数组, 容量和内部的value一样大.
*
*       for (int i = 0; i < value.length; i++) {
*           result[i] = value[i];
*       }
*
*       System.arraycopy(value, 0, result, 0, value.length);
*       // 第一个参数是源数组对象, 第二个参数是源数组的开始下标, 第三个参数是目标数组对象, 第四个参数是目标数组的开始下标
*       // 第五个参数是要复制的元素个数.
* public boolean equals(Object anObject)
* public boolean equalsIgnoreCase(String anotherString) 比较字符串内容, 忽略大小写
* public int compareTo(String anotherString)
*
*                  0 2       10       15       21        27   32 34
* String string = "  abcAQQY 我喜欢你,你喜欢我吗?我不喜欢你 zzz123  ";
*
* #####
* public int indexOf(String s), 获取参数中的子串在当前字符串中首次出现的下标. 如果不存在返回-1
*              string.indexOf("喜欢") => 11
*              string.indexOf("讨厌")
* public int indexOf(String s ,int startpoint)获取参数中的子串在当前字符串中首次出现的下标. 从startPoint位置开始搜索
*              string.indexOf("喜欢", 12) => 16, string.indexOf("喜欢", 17) => 23
*
* public int lastIndexOf(String s)
*              string.lastIndexOf("喜欢") => 23
* public int lastIndexOf(String s ,int startpoint)
*              string.lastIndexOf("喜欢", 22) => 16, string.lastIndexOf("喜欢", 15) => 11
*
* public boolean startsWith(String prefix) 判断当前串是否以参数中的子串为开始
* public boolean endsWith(String suffix)判断当前串是否以参数中的子串为结束
*
*                  0 2       10       15       21        27   32 34
* String string = "  abcAQQY 我喜欢你,你喜欢我吗?我不喜欢你 zzz123  ";
*
* #####
* public String substring(int start,int end) 从当前字符串中取子串, 从start开始(包含),到end结束(不包含)
*              string.substring(10, 14) => "我喜欢你";
* public String substring(int startpoint) 从当前字符串中取子串, 从start开始(包含),到最后
*
* public String replace(char oldChar,char newChar) 替换字符串中的所有oldChar为newChar
*              string.replace(' ', '#') =>
*
* // 参数中的字符串符合 正则表达式.
* public String replaceAll(String old,String new) 替换字符串中的所有old子串为new子串.
*
* public String trim() 修剪字符串首尾的空白字符. 空白字符就是码值小于等于32的字符
*
* public String concat(String str) 字符串连接
* public String toUpperCase() 变成大写
* public String toLowerCase() 变成小写
* String s = "abc,yy,123,qq,我和你,zzz";
* String[] arr = s.split(",");
* public String[] split(String regex) 以参数 中的子串为切割器,把字符串切割成多个部分.
*/
           

StringBuilder(StringBuffer)类

/**
 * StringBuilder : 是内容可以改变的Unicode字符序列.
 * <p>
 * 1. StringBuffer()初始容量为16的字符串缓冲区
 * 2.StringBuffer(int size)构造指定容量的字符串缓冲区
 * 3.StringBuffer(String str)将内容初始化为指定字符串内容
 * <p>
 * StringBuilder append(...) 追加任意数据到当前串末尾.
 * StringBuilder insert(int index, ...) 在指定位置处插入任意内容
 * StringBuilder delete(int beginIndex, int endIndex) 删除一个区间
 * StringBuilder setCharAt(int index, char ch) 替换指定下标处的字符为ch
 * <p>
 * private int newCapacity(int minCapacity) {
 * // overflow-conscious code
 * int newCapacity = (value.length << 1) + 2;
 * if (newCapacity - minCapacity < 0) {
 * newCapacity = minCapacity;
 * }
 * return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
 * ? hugeCapacity(minCapacity)
 * : newCapacity;
 * }
 * <p>
 * StringBuffer : 古老的类 速度慢 线程安全, 不使用
 * StringBuilder  : 新的类 速度快 线程不安全.
 */
           

日期类

// 当无法创建对象时, 有2种做法
// 1) 找当前类的静态方法, 看有没有能返回对象的方法
// 2) 找它的工厂类, 通过工厂获取对象.

/**
 * Date : 年有1900, 月也是少1, toString()不友好
 * Calendar : 内部用数组, 增加复杂度, 内容可以改变, 不好, 月份少1.
 *
 * java8 一步到位.
 * LocalDate 日期
 * LocalTime 时间
 * LocalDateTime 日期时间
 *
 */

public class DateTest {

    public static void main(String[] args) {
        LocalTime localTime = LocalTime.now(); //new LocalTime();
        System.out.println(localTime);

        LocalTime localTime1 = localTime.withHour(20);
        System.out.println(localTime1);

        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime);

        // 如何一步到位创建日期
        // 2008, 8, 8
        //new LocalDate(2008,8,8);
        LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 30, 40);
        System.out.println(of);

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String format = sdf.format(of);
        //System.out.println(format);

        //new DateTimeFormatter();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(of);
        System.out.println(format);
    }

    public static void main6(String[] args) {
        LocalDate date = LocalDate.now(); //new LocalDate();
        System.out.println(date);
        int year = date.getYear();
        int month = date.getMonthValue();
        int day = date.getDayOfMonth();

        System.out.println("year = " + year);
        System.out.println("month = " + month);
        System.out.println("day = " + day);

        //date.setYear(2008);
        // 伴随新的年会产生新对象
        LocalDate date2 = date.withYear(2008).withMonth(8).withDayOfMonth(8);
        System.out.println(date2);

        LocalDate date3 = date2.plusYears(5);

        System.out.println(date3);
        // 创建一个LocalDate对象, 把它变成你的生日. 然后再获取你的百日.
    }


    public static void main5(String[] args) {
        // 创建一个Calendar对象, 把它变成你的生日. 然后再获取你的百日.
        Calendar instance = Calendar.getInstance();
        // 1978, 6, 9
        instance.set(Calendar.YEAR, 1978);
        instance.set(Calendar.MONTH, 5);
        instance.set(Calendar.DAY_OF_MONTH, 9);

        System.out.println(instance.getTime());

        instance.add(Calendar.DAY_OF_MONTH, 100);

        System.out.println(instance.getTime());
    }

    public static void main4(String[] args) {
        // Calendar日历.
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);

        // 获取年
        int year = calendar.get(Calendar.YEAR); // 没有单独的获取方法
        int month = calendar.get(Calendar.MONTH); // 内部保存的月比实际小1.
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println("year = " + year);;
        System.out.println("month = " + month);
        System.out.println("day = " + day);

        // 设置. 2008, 8, 8
        //calendar.setYear(2008);
        calendar.set(Calendar.YEAR, 2008); // 没有单独的设置方法
        calendar.set(Calendar.MONTH, 7);
        calendar.set(Calendar.DAY_OF_MONTH, 8);

        Date time = calendar.getTime();
        System.out.println(time);

        // 加减
        calendar.add(Calendar.MONTH, 5); // 5个月后

        System.out.println(calendar.getTime());

        calendar.add(Calendar.DAY_OF_MONTH, -500);
        System.out.println(calendar.getTime());

    }

    public static void main3(String[] args) {
        // 北京奥运会 2008, 8, 8
        Date date = new Date(2008, 8, 8); // 直接创建对象会有问题
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));
    }

    public static void main2(String[] args) {
        long millis = System.currentTimeMillis();
        System.out.println(millis);

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

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S E");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date); // 把日期对象格式化成和模式一致的串
        System.out.println(format);

        String str = "2018-05-02 11:20:30";
        try {
            Date parse = sdf.parse(str); // 把字符串解析成日期对象
            System.out.println(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String format1 = sdf.format(millis); // 格式化毫秒
        System.out.println(format1);

    }
}