Java学习(06)--System 类/Date 类/ Calendar类

System 

(1)系统类 ,提供了一些有用的字段和方法

(2)成员方法 

A:运行垃圾回收器

public static void gc()

B:退出 jvm

public static void exit(int status):

终止当前正在运行的 Java虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。

C:获取当前时间的毫秒值

public static long currentTimeMillis()

D:数组复制

public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

从指定源数组中复制一个数组, 复制从指定的位置开始, 到目标数组的指定位置结束。

示例:

int [] arr = { 11, 22, 33, 44, 55 };

int [] arr2 = { 6, 7, 8, 9, 10 };

System.arraycopy (arr, 1, arr2, 2, 2);

System. out .println(Arrays.toString (arr));   //[11, 22, 33, 44, 55]

System.out .println(Arrays.toString (arr2));  //[6, 7, 22, 33, 10]


Date/DateFormat

(1) Date 是日期类,可以精确到毫秒。

A:构造方法

Date() Date(long time)

B:成员方法

getTime() setTime(long time)

C:日期和毫秒值的相互转换

案例:你来到这个世界多少天了 ?

代码:

Scanner sc =	newScanner(System.	in );  //	键盘录入你的出生的年月日
System.	out .println(	" 请输入你的出生年月日	:" ); 

String line = sc.nextLine();

SimpleDateFormat sdf =	newSimpleDateFormat(	"yyyy-MM-dd"	); //	把该字符串转换为一个日期
Date d = sdf.parse(line);

long	myTime = d.getTime();   //	通过该日期得到一个毫秒值

long	nowTime = System.	currentTimeMillis	();  //	获取当前时间的毫秒值

long	time = nowTime - myTime;  //	用  D-C得到一个毫秒值

long	day = time / 1000 / 60 / 60 / 24;  //	把 E的毫秒值转换为年

System.	out .println(	" 你来到这个世界:	"  + day +	" 天" );

(1) DateFormat 针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,

所以使用其子类 SimpleDateFormat

A:SimpleDateFormat(String pattern)   给定模式

yyyy-MM-dd HH:mm:ss

B:日期和字符串的转换

a:Date -- String 

format()

b:String -- Date

parse()

代码:
// Date -- String

Date d =	new Date();  //	创建日期对象

// SimpleDateFormat sdf = new SimpleDateFormat();    //	创建格式化对象

SimpleDateFormat sdf =newSimpleDateFormat(	"yyyy	年 MM月dd 日 HH:mm:ss"	);    //给定模式


// public final String format(Date date) String s = sdf.format(d);

System.	out .println(s);



//String -- Date

String str =	"2008-08-08 12:12:12"	;

//在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配

SimpleDateFormat sdf2 =	newSimpleDateFormat(	"yyyy-MM-dd HH:mm:ss"	); 

Date dd = sdf2.parse(str);

System.	out .println(dd);

 Calendar

(1)日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。

(2)如何得到一个日历对象呢 ?

Calendar rightNow = Calendar.getInstance();本质返回的是子类对象

(3)成员方法

A:根据日历字段得到对应的值 

// 其日历字段已由当前日期和时间初始化:

Calendar rightNow = Calendar.getInstance(); // 子类对象

int year = rightNow.get(Calendar.YEAR); // 获取年

int month = rightNow.get(Calendar.MONTH); // 获取月

int date = rightNow.get(Calendar.DATE); // 获取日 


B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值

public void add(int field,int amount):

根据给定的日历字段和对应的时间,来对当前的日历进行操作。

// 5 年后的 10 天前

c.add(Calendar.YEAR, 5);

c.add(Calendar.DATE, -10);

 

C:设置日历对象的年月日

public final void set(int year,int month,int date):

设置当前日历的年月日如: c.set(2011, 11, 1);

(4)案例:

计算任意一年的 2月份有多少天 ?

//键盘录入任意的年份

Scanner sc =newScanner(System.in);

System.out.println(" 请输入年份:");

int year = sc.nextInt();    //设置日历对象的年月日

Calendar c = Calendar.getInstance();

c.set(year, 2, 1);    // 其实是这一年的3月 1日,把时间往前推一天,就是2月的最后一天
System.	out .println(c.get(Calendar.MONTH)); 

c.add(Calendar.DATE, -1);

System.	out .println(c.get(Calendar.MONTH));    //获取这一天输出即可

System.	out .println(c.get(Calendar.DATE));

BigInteger 

(1)可以让超过 Integer 范围内的数据进行运算

(2)构造方法A:BigInteger(String s)

(3)成员方法 

public BigInteger add(BigInteger val):       //

public BigInteger subtract(BigInteger val):   //减

public BigInteger multiply(BigInteger val):   //乘

public BigInteger divide(BigInteger val):   //除

public BigInteger[] divideAndRemainder(BigInteger val) ://返回商和余数的数组


BigDecimal

(1)浮点数据做运算, 会丢失精度。 所以,针对浮点数据的操作建议采用 BigDecimal。

(2)构造方法

A:BigDecimal(String s)

(3)成员方法:

public BigDecimal add(BigDecimal augend)    //加

public BigDecimal subtract(BigDecimal subtrahend) //减

public BigDecimal multiply(BigDecimal multiplicand)  //乘

public BigDecimal divide(BigDecimal divisor)  //除

public BigDecimal divide(BigDecimal divisor,int scale,introundingMode) //商,几位小数






猜你喜欢

转载自blog.csdn.net/jianghao233/article/details/80874688