1 System类
其中的属性和方法都是静态的,不能被实例化。
常见应用:在跨平台操作时,不同平台有不同的快捷键。在System.getProperties方法中能获取当前的系统属性。Properties的实现是hashtable,有键值映射关系。而键是不会变化的,我们在跨平台操作时,可以通过(String value) System.getPropety(String key)得到value值,后自己定义一个String类型把这个值记录下来,下次用的时候直接用自己定义的String类型,提高了移值性
import java.util.Properties;
import java.util.Set;
public class SystemClass {
private static final String huiche=System.getProperty("line.separator");
public static void main(String []args)
{
method2();
}
public static void method1()
{
Properties p=System.getProperties();
Set<String> st=p.stringPropertyNames();
for(String s:st)
{
String value=p.getProperty(s);
System.out.println(s+":"+ value);
}
}
public static void method2()
{
System.out.println("asdfsaf"+huiche+"das");
System.out.println("A回车键b"+"A"+System.getProperty("line.separator")+"b");
}
}
2.Runtime类
无构造函数,不能创建对象,有非静态方法,说明类中有提供返回该类对象实例的静态方法,而且只有一个,使用了单例设计模式。常见用法如下
import java.io.IOException;
public class Runtimeclass {
public static void main(String []args) throws IOException, InterruptedException
{
Runtime rt=Runtime.getRuntime();
Process p=rt.exec("notepad");
Thread.sleep(300);
p.destroy();
}
}
3.Math类,Random类
常见用法如下
public class MathandRandomclass {
public static void main(String []args)
{
extrated2();
}
private static void extrated2()
{
Random r=new Random();
for(int i=0;i<10;i++)
System.out.println(r.nextInt(10)+1);
}
private static void extracted() {
System.out.println(Math.ceil(312.43));
System.out.println(Math.floor(312.43));
System.out.println(Math.round(312.43));
for(int i=0;i<10;i++)
System.out.println((int)(Math.random()*10+1));
}
}
4 Date类
1.日期对象和毫秒值直接的转换
1.1毫秒值->日期对象
①通过构造方法Date(long mi);
②通过SetTine方法进行设置。
目的:可以通过对象的方法对该日期的各个字段进行操作
1.2 日期对象->毫秒值
①通过getTime方法
目的:可以通过具体的数值进行运算。
1.3DateFormat类 通过静态方法返回对象的实例
可以对日期对象进行格式化,其中常用的有
①(String)format(Date date)方法能将日期格式化返回一段字符串。
②(Date )parse(String str)方法能将字符串转化为日期对象。
1.4SimpleDateFormat类
DateFormat的子类实现类,可以在外部直接创建对象,并在构造器中传入自定义的格式。其他用法和DateFormat类一致。
范例
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DataclassDemo1 {
public static void main(String []args) throws ParseException
{
method6();
}
public static void method1()
{
long ml=System.currentTimeMillis();
Date date=new Date(ml);
System.out.println(date);
}
public static void method2()
{
Date date2 =new Date();
date2.setTime(System.currentTimeMillis());
System.out.println(date2);
}
public static void method3()
{
Date date3=new Date();
long l=date3.getTime();
System.out.println(l);
}
public static void method4()
{
Date date4=new Date();
DateFormat df=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT);
String str=df.format(date4);
System.out.println(str);
}
public static void method5()
{
Date date5=new Date();
DateFormat df2=new SimpleDateFormat("yy年--MM月--DD日");
String str2=df2.format(date5);
System.out.println(str2);
}
public static void method6() throws ParseException
{
String str="2018年12月10日";
DateFormat df4=new SimpleDateFormat("yyyy年MM月DD日");
Date date7=df4.parse(str);
System.out.println(date7);
/*
* Exception in thread "main" java.text.ParseException: Unparseable date: "2019年12月10日"
* DateFormat df3=DateFormat.getDateInstance();
Date date6=df3.parse(str);
*/
}
}
5.calendar类
内部由map实现,其中键代表当前时间的属性,每一个键都对应相应的值。
其中add方法可以对时间进行偏移修改
范例
private static void JudgeFebdays( int year)//判断某一年二月有多少天
{
Calendar c1=Calendar.getInstance();
c1.set(year, 2, 1);
c1.add(c1.DATE, -1);
int days=c1.get(c1.DATE);
System.out.println(days);
}
private static void showdate() {
Calendar c=Calendar.getInstance();
int y=c.get(Calendar.YEAR);
int m=c.get(Calendar.MONTH)+1;
int d=c.get(Calendar.DATE);
System.out.println(y+"年"+m+"月"+d+"日");
}
}