Java基础 System,Runtime,Date,Calendar,Math-Random,Io流

System

import java.util.*;

class CollectionToArray
{
    public static void main(String[] args)
    {
        ArrayList<String> alist=new ArrayList<String>();

        alist.add("abc1");
        alist.add("abc2");
        alist.add("abc3");

        /*
        1.指定类型的数组长度
        指定类型的数组长度小于了集合的size,那么方法内部会创建一个
        新的数组长度为集合的size
        当指定类型的数组长度大于了集合的size,就不会新创建数组,而是使用传递进来的数组
        所以创建一个刚刚好的数组最优。
        2.为什么要集合变数组
        为了限定对元素的操作。
        */
        String sArr[]=new String[alist.size()];


        String[] arr=alist.toArray(sArr);
        System.out.println(Arrays.toString(arr));
    }
}

Runtime

/*
Runtime对象
该类并没有提供构造函数
说明不可以new对象。那么会直接想到该类中的方法都是静态的
发现该类中还有非静态方法
说明该类肯定会提供方法获取本类对象。而且方法是静态的,并返回值类型是本类类型。

该类使用了单例设计模式。

该方法是static Runtime getRuntime();
*/


class RuntimeDemo
{
    public static void main(String[] args)
    {
        Runtime rt=Runtime.getRuntime();
        Process p=null;
        try
        {
            p=rt.exec(new String[]{"notepad.exe","hello.txt"});
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        //wait(2000);
        try{
            Thread.currentThread().sleep(2000);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        p.destroy();
    }
}

Date

import java.util.*;
import java.text.SimpleDateFormat;
class DateDemo
{
    public static void main(String[] args)
    {
        Date dt=new Date();
        System.out.println(dt);
        //将模式封装到SimpleDateFormat对象中.
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 E HH:mm:ss");
        String time=sdf.format(dt);
        System.out.println(time);
    }
}

Calendar

import java.util.*;
class CalendarTest
{
    public static void main(String[] args)
    {
        Calendar c=Calendar.getInstance();
        //daysOfMonth(c,2015,2);
        yesterday(c);
        printCalendar(c);
    }
    public static void yesterday(Calendar c)
    {
        c.set(c.DAY_OF_YEAR,c.get(c.DAY_OF_YEAR)-1);

    }
    public static void printCalendar(Calendar c)
    {
        String[] mon=new String[]{"一月","二月","三月","四月",
        "五月","六月","七月","八月",
        "九月","十月","十一月","十二月"};

        String[] week=new String[]{"","星期天","星期一","星期二","星期三","星期四","星期五","星期六"};



        int imon=c.get(Calendar.MONTH);
        int iweek=c.get(c.DAY_OF_WEEK);
        sop(c.get(c.YEAR)+"年");
        sop(mon[imon]);
        sop(c.get(c.DAY_OF_MONTH));
        sop(week[iweek]);
    }
    public static void daysOfMonth(Calendar c,int year,int month)
    {
        c.set(2015,month,1);
        int day2=c.get(Calendar.DAY_OF_YEAR);
        c.set(2015,month-1,1);
        int day1=c.get(Calendar.DAY_OF_YEAR);
        int day=day2-day1;
        sop(day);
    }
    public static void sop(Object obj)
    {
        System.out.println(obj);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值