常用API

作者: 吃瓜群众liu | 来源:发表于2016-12-27 20:10 被阅读0次

    Math:提供了操作数学运算的方法。都是静态的。

    常用的方法:
    ceil():返回大于参数的最小整数。
    floor():返回小于参数的最大整数。
    round():返回四舍五入的整数。
    pow(a,b):a的b次方。

    返回一个随机数:
    Math.random():返回一个大于等于0,小于1的随机数,是带正号的double型

    int d = (int)(Math.random()*6)+1;
    

    Random r = new Random();
    调用nextInt()、nextDouble()、nextLong()、nextFloat()、等

    Random r = new Random();
    int d = r.nextInt(6)+1;
    

    Runtime:没有构造方法摘要,说明该类不可以创建对象。
    又发现还有非静态的方法。说明该类应该提供静态的返回该类对象的方法。
    而且只有一个,说明Runtime类使用了单例设计模式。

          Runtime r = Runtime.getRuntime();
            
    //      execute: 执行。 xxx.exe 
            
            Process p = r.exec("notepad.exe");
            Thread.sleep(5000);
            p.destroy();//关进程
    

    System:类中的方法和属性都是静态的。

    常见方法:
    long currentTimeMillis();获取当前时间的毫秒值。

    getProperties()

    //获取系统的属性信息,并存储到了Properties集合中。 
             * properties集合中存储都是String类型的键和值。
             * 最好使用它自己的存储和取出的方法来完成元素的操作。
             */
            Properties prop = System.getProperties();
    Set<String> nameSet = prop.stringPropertyNames();
            
            for(String name : nameSet){
                String value = prop.getProperty(name);
                
                System.out.println(name+"::"+value);
            }
    

    Date类

    日期对象和毫秒值之间的转换。

    #######毫秒值-->日期对象 :
    1,通过Date对象的构造方法 new Date(timeMillis);
    2,还可以通过setTime设置。
    因为可以通过Date对象的方法对该日期中的各个字段(年月日等)进行操作。

    ####### 日期对象-->毫秒值:
    1,getTime方法。
    因为可以通过具体的数值进行运算。
    代码演示:

    Date date = new Date();//将当前日期和时间封装成Date对象。
    System.out.println(date);
    

    结果:

    Fri Dec 23 19:17:32 CST 2016

    Date date2 = new Date(1335664696656l);//将指定毫秒值封装成Date对象。
    System.out.println(date2);//返回从1970年1月1日开始走过指定毫秒数后的时间
    

    结果:

    Sun Apr 29 09:58:16 CST 2012

    Date date = new Date();//将当前日期和时间封装成Date对象。
    System.out.println(date.getTime());//返回毫秒值
    

    结果:

    1482492087403


    对日期对象进行格式化。
    • 将日期对象-->日期格式的字符串。
    • 使用的是DateFormat类中的format方法。
    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateInstance();
    System.out.println(dateFormat.format(date));//默认风格
    

    结果:

    2016-12-23

    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);//FULL风格
    System.out.println(dateFormat.format(date));
    

    结果:

    2016年12月23日 星期五

    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
    System.out.println(dateFormat.format(date));
    

    结果:

    2016年12月23日

    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
    System.out.println(dateFormat.format(date));
    

    结果:

    2016年12月23日 下午07时31分50秒

    //带时分秒的格式
    DateFormat dateFormat = DateFormat.getDateTimeInstance();//默认格式
    System.out.println(dateFormat.format(date));
    

    结果:

    2016-12-23 19:32:45

    自定义风格
    DateFormat dateFormat = new SimpleDateFormat("yyyy--MM--dd::HH:mm:ss");
            
    String str_date = dateFormat.format(date);
            
    System.out.println(str_date);
    

    结果:

    2016--12--23::19:36:46


    将日期格式的字符串-->日期对象。

    使用的是DateFormat类中的parse()方法。

    String str_date = "2012年4月19日";
    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
    Date date = dateFormat.parse(str_date);
    System.out.println(date);
    

    结果:(注意风格要对应,不然无法识别字符串,这是解析默认格式的)

    Thu Apr 19 00:00:00 CST 2012

    String str_date = "2011---8---17";
    DateFormat dateFormat = new SimpleDateFormat("yyyy---MM---dd");     
    Date date = dateFormat.parse(str_date);
    System.out.println(date);
    

    结果:(注意:这是解析的自定义时间格式的)

    Wed Aug 17 00:00:00 CST 2011


    Calendar

    通过getInstance()方法来获得日历对象
    Calendar c = Calendar.getInstance();

    int get(int field):返回给定日历字段的值

    Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH)+1;
    int day = c.get(Calendar.DAY_OF_MONTH);
    int week = c.get(Calendar.DAY_OF_WEEK);
    System.out.println(year+"年"+month+"月"+day+"日"+week);
    

    结果:

    2016年12月23日6

    set方法和add方法
    void set(int year, int month, int date)
    add(itn field,int amount)//指定字段做时间的偏移,amount为正数是加,负数为减

    public static void main(String[] args) {
    
            Calendar c = Calendar.getInstance();
            
            int year = 2012;
            showDays(year);
        }
    public static void showDays(int year) {
            
            Calendar c = Calendar.getInstance();
            c.set(year, 2, 1);//这里是3月,月份从0开始计数,设置日期为2012年3月1日
            c.add(Calendar.DAY_OF_MONTH, -1);//将当前设置的日向前偏移1天,指3月1日的前一天
            
            showDate(c);
        }
    
        public static void showDate(Calendar c) {
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH)+1;//月份是从0开始到11
            int day = c.get(Calendar.DAY_OF_MONTH);
            int week = c.get(Calendar.DAY_OF_WEEK);
            
            
            System.out.println(year+"年"+month+"月"+day+"日"+week);
        }
    
    

    结果:

    2012年2月29日4

    相关文章

      网友评论

          本文标题:常用API

          本文链接:https://www.haomeiwen.com/subject/vmnkvttx.html