System类
1.System:类中的方法和属性都是静态的。
2.常见方法: long currentTimeMillis();获取当前时间的毫秒值。
3.//必须掌握 确定当前系统属性 getProperties java是跨平台的。
//public static Properties getProperties()
package day20;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
properties();
}
public static void properties(){
Properties properties =System.getProperties();
System.out.println(properties);
for (Map.Entry<Object,Object> me:
properties.entrySet()
) {
System.out.println(" key ="+me.getKey()+" value ="+me.getValue());
}
}
}
/****
*
* key =sun.desktop value =windows
* key =awt.toolkit value =sun.awt.windows.WToolkit
* key =java.specification.version value =11
* key =sun.cpu.isalist value =amd64
* key =sun.jnu.encoding value =GBK
* key =java.class.path value =E:\java\20190420_1\out\production\20190420_1
* key =java.vm.vendor value =JetBrains s.r.o
* key =sun.arch.data.model value =64
* key =user.variant value =
* key =java.vendor.url value =https://www.jetbrains.com/
* key =user.timezone value =
* key =os.name value =Windows 10
* key =java.vm.specification.version value =11
* key =sun.java.launcher value =SUN_STANDARD
* key =user.country value =CN
* key =sun.boot.library.path value =D:\idea\IntelliJ IDEA Community Edition 2019.1.1\jre64\bin
* key =sun.java.command value =day20.PropertiesDemo
* key =jdk.debug value =release
* key =sun.cpu.endian value =little
* key =user.home value =C:\Users\zengjx
* key =user.language value =zh
* key =java.specification.vendor value =Oracle Corporation
* key =java.version.date value =2019-01-15
* key =java.home value =D:\idea\IntelliJ IDEA Community Edition 2019.1.1\jre64
* key =file.separator value =\
* key =java.vm.compressedOopsMode value =32-bit
* key =line.separator value =
*
* key =java.specification.name value =Java Platform API Specification
* key =java.vm.specification.vendor value =Oracle Corporation
* key =java.awt.graphicsenv value =sun.awt.Win32GraphicsEnvironment
* key =user.script value =
* key =sun.management.compiler value =HotSpot 64-Bit Tiered Compilers
* key =java.runtime.version value =11.0.2+9-b159.44
* key =user.name value =zengjx
* key =path.separator value =;
* key =os.version value =10.0
* key =java.runtime.name value =OpenJDK Runtime Environment
* key =file.encoding value =UTF-8
* key =java.vm.name value =OpenJDK 64-Bit Server VM
* key =java.vendor.url.bug value =https://youtrack.jetbrains.com
* key =java.io.tmpdir value =C:\Users\zengjx\AppData\Local\Temp\
* key =java.version value =11.0.2
* key =user.dir value =E:\java\20190420_1
* key =os.arch value =amd64
* key =java.vm.specification.name value =Java Virtual Machine Specification
* key =java.awt.printerjob value =sun.awt.windows.WPrinterJob
* key =sun.os.patch.level value =
* key =java.library.path value =D:\idea\IntelliJ IDEA Community Edition 2019.1.1\jre64\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:\python\Scripts\;D:\python\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\git\Git\cmd;D:\TortoiseSVN\bin;D:\svnserver\bin;D:\tomcat\apache-tomcat-9.0.16\bin;D:\Android\Sdk\platform-tools;D:\nodejs\;D:\Java\jdk9\bin;C:\Users\zengjx\AppData\Local\Microsoft\WindowsApps;C:\Users\zengjx\AppData\Roaming\npm;.
* key =java.vendor value =JetBrains s.r.o
* key =java.vm.info value =mixed mode, sharing
* key =java.vm.version value =11.0.2+9-b159.44
* key =sun.io.unicode.encoding value =UnicodeLittle
* key =java.class.version value =55.0
*/
public static void properties_1(){
Properties properties =System.getProperties();
Set<String > nameSet=properties.stringPropertyNames();
System.out.println(properties);
for ( String s:
nameSet
) {
System.out.println(" key ="+s+" value ="+properties.getProperty(s));
}
}
key =sun.desktop value =windows
key =awt.toolkit value =sun.awt.windows.WToolkit
key =java.specification.version value =11
key =sun.cpu.isalist value =amd64
key =sun.jnu.encoding value =GBK
key =java.class.path value =E:\java\20190420_1\out\production\20190420_1
key =java.vm.vendor value =JetBrains s.r.o
key =sun.arch.data.model value =64
key =user.variant value =
操作系统不一样,换行不一样:
windows :System.out.print("Hello \r\n");
linux:System.out.print("Hello \n");
使用:
private static final String LINE_SEPARATOR = System.getProperty("line.separator");在任何系统中换行
private static void line(){
System.out.print("hello"+LINE_SEPARATOR+"world");
}
//给系统设置一些属性信息。这些信息是【全局】,其他程序都可以使用。
// System.setProperty("myclasspath", "c:\myclass");
Runtime类;
构造函数被私有化了,不能创建对象。
单例模式:
启动应用程序:
记事本打开指定文件:
rt.exec("notepad.exe C:\\Users\\zengjx\\Desktop\\新建文本文档 (6).txt" );
获取进程:
只能杀死该程序启动的进程:
package day20;
import java.io.IOException;
public class RuntimeDemo {
public static void main(String[] args)throws IOException {
Runtime rt= Runtime.getRuntime();
// rt.exec("D:\\360压缩\\Typeeasy\\TypeEasy.exe");
Process process= rt.exec("notepad.exe C:\\Users\\zengjx\\Desktop\\新建文本文档 (6).txt" );
// Process process= rt.exec("C:\\Program Files (x86)\\KuGou\\KGMusic\\KuGou.exe F:\\Kugou\\Carly Rae Jepsen - Picture.mp3" );
try{
Thread.sleep(5000);
}catch ( InterruptedException e){
e.printStackTrace();
}
process.destroy();
}
}
Math类;
全是静态方法。
for(int i=0;i<10;i++){
System.out.println((int )(6*Math.random()+1));
}
随机数对象 :
for(int i=0;i<10;i++){
System.out.println(random.nextInt(100));[0,100) 之间
}
Date类(必须)
1.月份是 0-11
package day20;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
Date date =new Date();
System.out.println(date);//Sun Jun 02 21:42:50 CST 2019
//1.将毫秒之封装成对象 :通过Date 对象构造方法完成。还可以通过 setTime 设置
// 可以对Date 对象的方法 各个值年月日 操作。
long time =System.currentTimeMillis();
Date date1= new Date(time);
System.out.println(date1);//Sun Jun 02 21:44:30 CST 2019
//2.日期转为毫秒值:getTime
// 进行运算
System.out.println(date1.getTime());//1559483482724
//3.日期比较
System.out.println(date.compareTo(date1));//-1
System.out.println(date.after(date1));//false
//4.toString
System.out.println(date.toString());//Sun Jun 02 21:59:33 CST 2019
String str_date1 = "2012-3-17";
String str_date2 = "2012-4-18";
// Date date2 =new Date(str_date1);
}
}
Date类日期对象转字符串,
日期格式化:
//对日期对象格式化---转为日期字符串
DateFormat df= DateFormat.getDateInstance();//获取默认格式
String sDate= df.format(date);
System.out.println("日期对象格式化 :"+sDate);//日期对象格式化 :2019年6月2日
Date datefor= new Date();
DateFormat df2= DateFormat.getDateTimeInstance();//获取默认格式
String sDate2= df2.format(datefor);
System.out.println("日期对象格式化 :"+sDate2);//日期对象格式化 :2019年6月2日 下午10:22:09
//指定风格 DateFormat.SHORT 2019/6/2
DateFormat df3= DateFormat.getDateInstance(DateFormat.LONG);//指定风格日期对象格式化 :2019年6月2日星期日
String sDate3= df3.format(datefor);
System.out.println("指定风格日期对象格式化 :"+sDate3);
DateFormat df4= DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);//获取默认格式
String sDate4= df4.format(datefor);
System.out.println("DateFormat.LONG,DateFormat.SHORT指定风格日期对象格式化 :"+sDate4);//2019年6月2日 下午10:32
//自定义风格
-
public class SimpleDateFormat extends DateFormat
SimpleDateFormat
是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。 它允许格式化(日期文本),解析(文本日期)和归一化。
自定义风格
DateFormat dateFormt= new SimpleDateFormat("yyyy:MM.dd G 'at' HH:mm:ss z");
String sDate5= dateFormt.format(datefor);
System.out.println("SimpleDateFormat Date"+sDate5);//SimpleDateFormat Date2019:06.02 公元 at 22:41:11 CST
字符串转成日期对象
//日期字符串 转为对象
public static void method3() {
String str="2019年6月2日 下午10:32";
DateFormat dateFormat =DateFormat.getDateInstance(DateFormat.LONG);//Sun Jun 02 00:00:00 CST 2019
// DateFormat dateFormat =DateFormat.getDateInstance(DateFormat.SECOND_FIELD);//Exception in thread "main" java.lang.IllegalArgumentException: Illegal date style 7
String string ="2014-12-------12";
//自定义
DateFormat dateFormat1 =new SimpleDateFormat("yyyy-mm-------dd");
try{
Date date=dateFormat.parse(str);
System.out.println(date);
}catch ( ParseException e){
e.getErrorOffset();
}
try{
Date date2=dateFormat1.parse(string);
System.out.println(date2);
}catch ( ParseException e){
e.getErrorOffset();
}
}
/* 练习:
* "2012-3-17"到"2012-4-6"
* 中间有多少天?
* 思路:
* 两个日期相减就哦了。
* 咋减呢?
* 必须要有两个可以进行减法运算的数。
* 能减可以是毫秒值。如何获取毫秒值?通过date对象。
* 如何获取date对象呢?可以将字符串转成date对象。
*
* 1,将日期格式的字符串转成Date对象。
* 2,将Date对象转成毫秒值。
* 3,相减,在变成天数
*
*
*/
public class DateTest {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
String str_date1 = "2012-3-17";
String str_date2 = "2012-4-18";
test(str_date1,str_date2);
}
public static void test(String str_date1,String str_date2) throws ParseException {
//1,将日期字符串转成日期对象。
//定义日期格式对象。
DateFormat dateFormat = DateFormat.getDateInstance();
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dateFormat.parse(str_date1);
Date date2 = dateFormat.parse(str_date2);
long time1 = date1.getTime();
long time2 = date2.getTime();
long time = Math.abs(time1-time2);
int day = getDay(time);
System.out.println(day);
}
private static int getDay(long time) {
int day = (int)(time/1000/60/60/24);
return day;
}
}
// String str_date1 = "2012-3-17";
// String str_date2 = "2012-4-18";
// 将字符串日期转为日期对象
// 日期对象转为毫秒 再转为天 再相减
public static void test(){
String str_date1 = "2012-3-17";
String str_date2 = "2012-4-18";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try{
Date date1= dateFormat.parse(str_date1);
Date date2=dateFormat.parse(str_date2);
long time1=date2.getTime()-date1.getTime();
System.out.println("相差的天数"+time1);
time1=time1/(1000*60*60*24);
System.out.println("相差的天数"+time1);
} catch (ParseException e){
e.getErrorOffset();
}
}
Calendar 日期:
public static void canlendar(){
Calendar calendar =Calendar.getInstance();
System.out.println( calendar.get(Calendar.DATE));//2
System.out.println( calendar.get(Calendar.YEAR));//2019
System.out.println( calendar.get(Calendar.MONTH)+1);//6 要 加1
System.out.println( calendar.get(Calendar.DAY_OF_MONTH));//2
System.out.println( calendar.get(Calendar.DAY_OF_WEEK));// 1
//设置时间 日期偏移
calendar.set(2019,3,30);
calendar.add(Calendar.YEAR,2); // 加两年
System.out.println("偏移"+calendar.get(Calendar.YEAR));//2021
// 计算某一年 二月有几天 ?
int year =2011;
calendar.set(year,2,1);// 设置为3.1 日 再减1 天
calendar.add(Calendar.DAY_OF_MONTH,-1);
System.out.println( calendar.get(Calendar.DAY_OF_MONTH));// 28 天
}