本文主要是介绍RunTime、 Date、 SimpleDateFormat,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import java.io.IOException;/*Runtime类:代表了当前程序的运行环境。Runtime对象需要掌握方法:exec(String command) 执行指定路径下的可执行文件。*/
public class Demo3 {public static void main(String[] args) throws IOException, Exception {Runtime runtime = Runtime.getRuntime(); //获取Runtime对象/* Process p = runtime.exec("C:\\Windows\\notepad.exe");Thread.sleep(3000); //让当前程序暂停3秒钟p.destroy(); //杀死进程。*/System.out.println("试图使用的最大内存量:"+ runtime.maxMemory());System.out.println("Java 虚拟机中的内存总量:"+ runtime.totalMemory());System.out.println("当前空闲的内存:"+ runtime.freeMemory());}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;/*Date 日期类:Calendar 日期类SimpleDateFormat 日期格式化类:作用: 1. 可以将时间对象转成指定格式的字符串。 format();2. 可以把字符串转成日期对象。 parse();*/
public class Demo4 {public static void main(String[] args) throws Exception {/*Date date = new Date(); //获取当前系统时间的对象。System.out.println("年份:"+ );Calendar calendar = Calendar.getInstance(); //获取了当前的系统时间System.out.println("年份:"+ calendar.get(Calendar.YEAR));System.out.println("月份:"+ (calendar.get(Calendar.MONTH)+1));System.out.println("日:"+ calendar.get(Calendar.DATE));System.out.println("时:"+ calendar.get(Calendar.HOUR_OF_DAY));System.out.println("分:"+ calendar.get(Calendar.MINUTE));System.out.println("秒:"+ calendar.get(Calendar.SECOND));// xxxx年 xx月 xx日 xx:xx:xx*/SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); //创建日期格式化类对象//把日期对象转换指定格式的字符串 format/*String text = dateFormat.format(new Date());System.out.println("当前系统时间:"+ text);*///字符串----> 时间对象 字符串的格式必须要与SimpleDateformat指定的模式要一致,否则报错。String text = "1990年09月08日 07:01:00";Date date = dateFormat.parse(text);System.out.println(date);}}
这篇关于RunTime、 Date、 SimpleDateFormat的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!