代码改变世界

总结

2016-11-23 22:46  乐扬  阅读(98)  评论(0)    收藏  举报

目录
1. System类
2. String类
3. StringBuffer类
4. StringBuilder类
5. Date类
6. SimpleDateFormat类
7. Calendar类
8. Math类

System类

currentTimeMillis()

返回以毫秒为单位的当前时间

    long begin = System.currentTimeMillis();  
    long end = System.currentTimeMillis();  

exit(int status)

终止当前正在运行的Java虚拟机

    System.exit(0);  

String类

compareTo(String anotherString)

按字典顺序比较两个字符串,如果按字典顺序此 String 对象位于参数字符串之前,则比较结果为一个负整数。如果按字典顺序此 String 对象位于参数字符串之后,则比较结果为一个正整数。如果这两个字符串相等,则结果为 0。

    String s = "ab";
    String s1 ="ac";
    System.out.println(s.compareTo(s1));  

concat(String str)

将指定字符串连接到此字符串的结尾。

    String s = "hello";
    String s1 = "world";
    System.out.println(s.concat(s1));  

endsWith(String suffix)

测试此字符串是否以指定的后缀结束。 如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。

    String s = "123456@qq.com";
    System.out.println(s.endsWith("com"));  

equals(Object anObject)

将此字符串与指定的对象比较, 如果给定对象表示的 String 与此 String 相等,则返回 true;否则返回 false。

    String s1 = "1234";
    String s2 = "12345";
    System.out.println(s1.equals(s2));  

indexOf(String str)

返回指定子字符串在此字符串中第一次出现处的索引。如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1。

    String s1 = "1234";
    System.out.println(s1.indexOf("2"));

StringBuffer类

append(String str)

将指定的字符串追加到此字符序列

    StringBuffer s1 = new StringBuffer("hello");
    System.out.println(s1.append("wrold"));  

replace(int start, int end, String str)

使用给定 String 中的字符替换此序列的子字符串中的字符.该子字符串从指定的 start 处开始,一直到索引 end - 1 处的字符,如果不存在这种字符,则一直到序列尾部。先将子字符串中的字符移除,然后将指定的 String 插入 start。

    StringBuffer s1 = new StringBuffer("hello");
    System.out.println(s1.replace(0, 0, "张三"));  

StringBuilder类

insert(int offset, String str)

按顺序将 String 参数中的字符插入此序列中的指定位置,将该位置处原来的字符向后推,此序列将增加该参数的长度。

    StringBuilder s = new StringBuilder("hello");
    System.out.println(s.insert(4, "张三"));  

Date类

compareTo(Date anotherDate)

比较两个日期的顺序。如果参数 Date 等于此 Date,则返回值 0;如果此 Date 在 Date 参数之前,则返回小于 0 的值;如果此 Date 在 Date 参数之后,则返回大于 0 的值。

    Date d1 = new Date();
    Date d2 = new Date();
    System.out.println(d1.compareTo(d2));

getTime()

返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。

    Date d1 = new Date();
    System.out.println(d1.getTime());  

SimpleDateFormat类

format(Date date, StringBuffer toAppendTo, FieldPosition pos)

将给定的 Date 格式化为日期/时间字符串,并将结果添加到给定的 StringBuffer。

    Date d = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    System.out.println(sdf.format(d));

parse(String text, ParsePosition pos)

解析字符串的文本,生成 Date。从字符串进行解析的 Date。如果发生错误,则返回 null。

    String date2 ="2016-11-23-13-28-53";
    Date d2 = new Date();
    d2=sdf.parse(date2);     

Calendar类

add(int field, int amount)

根据日历的规则,为给定的日历字段添加或减去指定的时间量

Calendar calendar = new GregorianCalendar();
calendar.add(calendar.MONTH, 3);

set(int year, int month, int date)

设置日历字段 YEAR、MONTH 和 DAYOFMONTH 的值。

Calendar calendar = new GregorianCalendar();
calendar.set(2016,11,3);

setTime(Date date)

使用给定的 Date 设置此 Calendar 的时间。

    Calendar calendar = new GregorianCalendar();
    Date d = new Date();
    calendar.setTime(d);

Math类

abs(int a)

返回 int 值的绝对值。如果参数为非负数,则返回该参数。如果参数为负数,则返回该参数的相反数

    int a = -10;
    System.out.println(Math.abs(a));

round(double a)

返回最接近参数的 long。

    double a = 12.5;
    System.out.println(Math.round(a));

ceil(double a)

返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。

    double a = 12.1;
    System.out.println(Math.ceil(a));

floor(double a)

返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。

    double a = 11.9;
    System.out.println(Math.floor(a));

random()

返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。返回值是一个伪随机选择的数,在该范围内(近似)均匀分布。

    int a =  (int) (Math.random()*5+1);
    System.out.println(a);