zoukankan      html  css  js  c++  java
  • 【常用API】之常用方法:String、Math、Date

    ----String

    package com.xzm.常用方法;
    
    public class _01_字符串类String {
    
        public static void main(String[] args) {
            
            //字符串引用类型(类)
            String str = "ABCD-abcd-1234";
            
            //方法.length()        
            System.out.println("字符个数:" + str.length());
            
            //charAt(索引)
            System.out.println("索引0的字符:" + str.charAt(0));
            
            //替换
            System.out.println( str.replace("-", "★") );//全部        
            System.out.println( str.replaceFirst("-", "☻") );//第一个
            
            //判断:str中,是否包含字符串"abc": Boolean
            System.out.println( str.contains("abc") );
            
            
            //统一转换大小写
            System.out.println( str.toUpperCase() );
            System.out.println( str.toLowerCase() );
            
            
            //字符串转换成字符数组
            //每一个字都变成数组中的一个元素
            char[] arr1 = str.toCharArray();
            for(int i=0; i<arr1.length; i++) 
            {
                System.out.println( "arr1["+i+"]="+arr1[i]+";" );
            }
            
            
            
            //按照界定符,切割成字符串数组
            String[] arr2 = str.split("-");
            for(int i=0; i<arr2.length; i++) 
            {
                System.out.println( "arr2["+i+"]="+arr2[i]+";" );
            }
            
            
            //查找,返回指定内容的第一个字符所在的索引
            //没有就是 -1
            System.out.println("第一次-符号出现在索引:"+ str.indexOf("-") );
            
            System.out.println("最后一次-符号出现在索引:"+ str.lastIndexOf("-") );
            
            System.out.println("#符号有没有:"+ str.indexOf("#") );
            
            
            //==========================================
            
            str = "abcdefghijklmn";
            
            //截取:索引5开始,后面全部
            System.out.println( str.substring(5) );
            
            //截取:索引0开始,索引5-1结束
            System.out.println( str.substring(0, 5) );
            
            
            // equals判断内容是否相同:返回Boolean
            
            // String.format(),格式化字符串内容。
            
            // .***valuOf(),转换成其他数据类型。
            
    
        }
    
    }

    -----Math

    package com.xzm.常用方法;
    
    public class _02_科学计算Math {
    
        public static void main(String[] args) {
            
            //科学计算:Math
            
            //随机数:返回0-1之间,没有1的,随机浮点double
            System.out.println( Math.random() );
            
            //四舍五入:long 或 int
            System.out.println( Math.round(3.999) );
            
            
            //向上或向下取整:返回 double
            System.out.println( Math.ceil(4.00000001) );        
            System.out.println( Math.floor(5.9) );
    
            //次方:返回double,支持负数
            System.out.println( Math.pow(5,-8) );
            
        }
    
    }

    ----Date

    package com.xzm.常用方法;

    import java.util.Calendar;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;

    public class _03_日期时间 {

    public static void main(String[] args) {

    //基于 util常用工具包的日期类对象
    Date dt = new Date();

    //默认格式输出
    System.out.println(dt);

    //获取指定部分,得到的都是int

    //年:从1900年开始计算的。
    int year = dt.getYear();
    System.out.println("今年是:" + (1900+year));

    //月:0-11 getMonth()
    //日:1-31 getDate()
    //星期:0-6 getDay()
    //时间都正常的。
    //这些操作方式已经淘汰了,但是依然可以使用【正确的】

    //=============================================

    //使用新的对象 Calendar对象
    //里面都是可以自己设置操作。

    //对象 = 对象.静态方法();
    Calendar cl = Calendar.getInstance();

    //设置日期
    cl.set(Calendar.YEAR, 2020);

    //获取
    System.out.println(Calendar.YEAR);

    //=================================================
    //【了解:定时器,使用Calendar】


    //创建时间对象
    Calendar cd = Calendar.getInstance();

    //设置:每天12点正
    cd.set(Calendar.HOUR, 12);
    cd.set(Calendar.MINUTE, 0);
    cd.set(Calendar.SECOND, 0);

    //获取24小时对应的毫秒
    int peroid = 1000 * 60 * 60 * 24;


    //定义定时器
    Timer t = new Timer();
    //调用方法执行(处理类对象,日期,毫秒)
    t.scheduleAtFixedRate(new MyTask(), cl.getTime(), peroid);

    }

    }


    //===========================================
    // 定时器的方法
    class MyTask extends TimerTask{
    @Override
    public void run() {
    System.out.println("时间到了.......");
    }
    }

  • 相关阅读:
    pycharm 对mysql的可视化操作
    pycharm连接linux创建django工程
    linux上安装pycharm
    pycharm激活码
    Windows下安装pip
    migrate设置
    python相对目录的基本用法(一)
    pycharm设置连接github
    在shell终端操作oracle数据库的常用命令
    在windows中把一个文件夹打成war包
  • 原文地址:https://www.cnblogs.com/jiarenanhao/p/14134176.html
Copyright © 2011-2022 走看看