您的位置:首页 > 编程语言 > Java开发

Java的系统类:System、Runtime ;和时间类:Date、Calendar

2016-02-26 18:03 477 查看
1:System类的功能

获得系统的所有属性:System.getProperties()

获得系统属性中具体的属性:System.getProperty(“os_name”)

获得系统时间:System.currentTimeMillis()

这个时间转化为Date:new Date(System.currentTimeMillis());

设置系统属性:System.setProperty(“os_name”, “window8”)

2:Runtime类的功能

Runtime runtime = Runtime.getRuntime();

启动一个进程:Runtime.getRuntime().exec(“XXXX”);

获得系统空闲内存等:Runtime.getRuntime().freeMemory();

3:时间类 Date和Calendar

Java中有Date类的包有两个:java.util和java.sql,后者是前者的子类

Date类的方法很少,就是简单的获取:年、月、日以及时、分、秒还有就是获取毫秒数等。逐渐被功能更加强大的Calendar取代

Calendar类不能用new来创建实例,而是要通过他的静态方法getInstance()方法来获得,具有比Date更加丰富的功能方法

4:格式化类,格式化数字和格式化时间

格式化数字:NumberFormat类和DecimalFormat类

decimalFormat类功能比NumberFormat强大。可以定义规则

System.out.println(new DecimalFormat("000").format(232562));
System.out.println(new DecimalFormat("$,##0.0").format(1.2));
System.out.println(new
DecimalFormat(",##0.00%").format(0.225));


5:格式化日期simpleDateFormat类

一下几种使用方式:

===================将时间格式化为字符串================================

String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());

String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTimeInMillis());

String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime();


====================将字符串格式化为时间============================

这个主要是根据给出的字符串格式来创建SimpleDateFormat对象,然后调用parse方法即可

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-07-27 10:52:30");


以上都是简单的时间格式化转换,但是平时我们会遇到很多的英文日期时间字符串,如何把他们转化为date时间对象呢?

比如:30/May/2013:17:38:25 , 这是需要借助Locale.ENGLISH。其中月份需要三个“MMM”。

SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss",Locale.ENGLISH);
Date date = sdf.parse("30/May/2013:17:38:25");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息