JavaSE基础自学----- 其他类(System / Runtime / Math / Date)

System 类

import java.util.*;

public class DEMO2 {
    private static final String FILE_SEPARATOR = System.getProperty("file.separator"); //获取系统分隔符
    private static final String PATH_SEPARATOR = System.getProperty("path.separator"); //获取路径分隔符
    private static final String LINE_SEPARATOR = System.getProperty("line.separator"); //行分隔符

    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        System.out.println(time);

        Properties p = System.getProperties();  //获取系统属性集合

        Set<String> key = p.stringPropertyNames();
        for (String s : key) {
            String values = p.getProperty(String.valueOf(s));
            System.out.println(s + " : " + values);
        }

        System.out.println(p.getProperty("os.name"));
        System.out.println(System.getProperty("os.name"));

        System.out.println("c:" + FILE_SEPARATOR + "abc" + FILE_SEPARATOR + "d.text");

        System.out.println("c:" + PATH_SEPARATOR + "d:");

        System.out.println("Hallo" + LINE_SEPARATOR + "word");
    }
}

Runtime 类

      Runtime r=Runtime.getRuntime();
      
      //输入迅雷位置,打开迅雷
      r.exec("\"C:\\Program Files (x86)\\Thunder Network\\Thunder\\Program\\ThunderStart.exe\"-StartType:DesktopIcon");

      r.exec("notepad.exe C:\\Users\\jackchan\\Desktop\\gggg.txt");

Math 类

 System.out.println(Math.ceil(10.111)); //返回比该值大的整数

        System.out.println(Math.floor(10.111));//返回比该值小的整数

        System.out.println(Math.round(10.66));//返回该值四舍五入的值

        System.out.println(Math.max(99,99.5)); //返回最大的值

        System.out.println(Math.pow(10,3));  //10 的3 次方

        for (int i=0;i<20;i++){
            int d=(int)(Math.ceil(Math.random()*6));  //随机数
            System.out.println(d);
        }

        Random r=new Random();
        System.out.println(r.nextDouble());  //与Math.random() 用法一致
        System.out.println(r.nextBoolean());

Date 类

  Date date = new Date();
        System.out.println(date);

       //通过DateFormat类中的静态工厂方法获取实例 
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
        /*FULL : 2019年2月26日 星期二 上午12时26分19秒 CST
          LONG : 2019年2月26日 上午12时26分56秒
          MEDIUM : 2019-2-26 0:27:30
          SHORT : 19-2-26 上午12:28
         */

        df=new SimpleDateFormat("yy/MM/dd  E  hh:mm:ss"); //自定义日期格式
        
//使用DateFormat中的format方法对日期对象进行格式化 ,将日期对象转成日期格式的字符串
        String str_date = df.format(date);
        System.out.println(str_date);

        //解析日期
        String str="2017-02-25";
        DateFormat dff=DateFormat.getDateInstance();
        Date d=dff.parse(str);
        System.out.println(d);

        //毫秒值转成日期对象
        long time=1581666279260L;
        Date d1=new Date(time);
        String s=df.format(d1);
        System.out.println(s);

        //日期对象转成毫秒值
        long time1=d1.getTime();
        System.out.println(time1);

Calendar 类 日历

 Calendar c = Calendar.getInstance();
 
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        String week = getWeek(c.get(Calendar.DAY_OF_WEEK));
        System.out.println(year + "/" + month + "/" + day + "/" +week);

        c.set(2018,7,9);  //对日历对象中的日期进行自定义
        c.add(Calendar.MONTH,-1);//日期偏移

        //任意一年的2月有多少天
        int year=2014;
        c.set(year,2,1);
        c.add(Calendar.DAY_OF_MONTH,-1);



    private static String getWeek(int i) {
        if (i >= 7 || i <= 0) {
            throw new NotTheWeekException("无对应日期");
        }
        String[] s = {"", "星期七", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

        return s[i];
    }


         //2012/3/17   2012-5-28  算出日期相差几天
        String s1 = "2012-5-28";
        DateFormat df1 = DateFormat.getDateInstance(DateFormat.MEDIUM);
        Date d1 = df1.parse(s1);

        String s2 = "2012/3/17";
        DateFormat df2 = new SimpleDateFormat("yyyy/MM/dd");
        Date d2 = df2.parse(s2);

        long time1=d1.getTime();
        long time2=d2.getTime();
        System.out.println(time1);
        System.out.println(time2);
        long time3=time1-time2;
        System.out.println(time3/1000/60/60/24);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值