实用类

 日期类:
 现在讲的是 java.util.Date类  
 date()
 date(long mills)  

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

 Date date = new Date();  
 //默认调用toString方法
    System.out.println(date.toString());  

    //获取当前的系统的时间 毫秒数
    long time = System.currentTimeMillis();
    System.out.println("time : " + time); 

    //如何将一个Date对象变成一个其他格式的字符串  1990-02-03 12:20:30
    //SimpleDateFormat对象   Format:格式化
    //参数pattern : 日期格式
    //1.创建SimpleDateFormat对象 来定义日期格式 
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String s1 = dateFormat.format(date);
    System.out.println(s1+"\n");  
 }
 }  


1.生成随机数
RandomDemo 
Random random = new Random();
//nextInt(int number)随机生成一个0-number的值(不包含number)
for (int i = 0; i < 1000; i++) {
    System.out.println(random.nextInt(100)); 
}

2.生成随机数
Math类

class MathDemo {   
public static void main(String[] args) {  
//随机得到一个0-1的小数(不包含1)
double random = Math.random(); 
//其他的方法
     * abs()            求绝对值
     * max(int a, int b) 求最大值
     * min(int a, int b) 求最小值
     * ceil()           
     * floor()
     * round(double a)   

     * ceil()           
     * floor()
     * round(double a)
       double a = 3.14;
    //ceil 天花板  
    System.out.println(Math.ceil(a));
    //floor 地板 
    System.out.println(Math.floor(a));
    //round 周围附近 四舍五入
    System.out.println(Math.round(a));
}
}


object类 
方法1:equals();             比较内容 通过同一种方法运算出来的是一样的
方法2:hashCode();
方法3:toString();
方法4:getClass();

 方法1:equals();    
    ObjectDemo demo = new ObjectDemo(); 
        demo.age = 10;
        ObjectDemo demo1 = new ObjectDemo();
        demo1.age = 10;
        System.out.println("相等 ? : " + demo.equals(demo1));   //true   

方法2:    hashCode()
 就是使用了 哈希算法(也称散列算法) 


方法3:toString();   
 打印默认调用方法@左边为完整类名也是字节码文件名字@右边是哈希可码值   

方法4: getClass() 
因为一切皆是对象,类型也不例外,在Java使用类型类来表示一个类型。所有的类型类都是Class类的实例  

    Class clazz = demo.getClass();

    String name = clazz.getName();
    System.out.println(name);  


 * currentTimeMillis() //获取当前系统的时间 单位 : 毫秒值
 * 
 * exit(int status)        //退出虚拟机
 * 
 * gc();                       //回收java中的垃圾    

String类
获取
getBytes(); 获取一个字节数组
将一个字符串转化为一个字节数组byte[]的方法
getBytes(String charsetName)
length() 获取字符串长度
System.out.println(“length : ” + a.length());
charAt(int index); 获取该字符串对应下标的字符
indexOf(int ch) 获取该字符在该字符串中第一次出现的下标
lastIndexOf(int ch) 获取该字符在该字符串中最后一次出现的下标
截取
subString(int beginIndex) 截取从beginIndex到该字符串末尾的所有字符
subString(int beginIndex, int endIndex) 截取从beginIndex到endIndex(不包含endIndex)的所有 字符
包头不包尾
split(String regex); 以 regex这个字符串来切割 原字符串,获得一个新的字符串数组
trim();//去除字符串2端的空格(字符串中的空格不去除)

转换
toUpperCase() 将原字符串中的字母全部大写
toLowerCase() 将原字符串中的字母全部小写

判断
equals() 判断字符串中的内容是否相等
equalsIgnoreCase(String anotherString) 判断字符串中的内容是否相等(忽略字符的大小写)
startsWith() 判断原字符串是否以传入的字符串开头
endsWith(String suffix) 判断原字符串是否以传入的字符串为结尾
isEmpty() 判断字符串的长度是否为0
contains(String anotherString) 判断原字符串是否包含传入的字符串
compareTo(String anotherString) 比较字符串的大小(按照字典顺序abcdefg..)

连接
concat() 跟 + 一模一样的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值