Java?SimpleDateFormat與System類使用示例詳解
SimpleDateFormat類:
SimpleDateFormat是-一個以與語言環(huán)境有關(guān)的方式來格式化和解析日期的具體類。
進(jìn)行格式化(日期->文本)、解析(文本->日期)。
常用的時間模式字母

直接看代碼:
package com.classes.SimpleDateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo01 {
public static void main(String[] args) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyy年MM月dd日HH:mm:ss");
Date date = new Date();
// 把日期轉(zhuǎn)成字符串
String str = sdf.format(date);
System.out.println(str);
// 把字符串轉(zhuǎn)換成日期
Date date1= sdf.parse("2019年12月12日19:19:19");
System.out.println(date1.toLocaleString());
}
}運(yùn)行結(jié)果:

System類:
System系統(tǒng)類,主要用于獲取系統(tǒng)的屬性數(shù)據(jù)和其他操作。構(gòu)造方法私有的,不需要創(chuàng)建對象。
System.out 和System.in不多做贅述。
主要看它4個方法:
| 方法名 | 說明 |
| static void arraycopy(..) | 復(fù)制數(shù)組 |
| static long currentTimeMills(); | 獲取當(dāng)前系統(tǒng)時間,返回的是毫秒數(shù) |
| static void gc(); | 建議jvm啟動垃圾回收器回收垃圾 |
| static void exit(int status) | 退出jvm |
直接看代碼:
主方法:
package com.classes.system;
public class Demo01 {
public static void main(String[] args) {
// System類是私有的,不用new
// 1.arraycopy:數(shù)組復(fù)制
// 5個參數(shù):src:源數(shù)組 srcPos:從哪個位置開始復(fù)制 dest:目標(biāo)數(shù)組 destPos:目標(biāo)數(shù)組的位置 length:目標(biāo)長度
int [] arr = {22,18,15,8,35,26,45,90};
int [] dest = new int[8];
System.arraycopy(arr,0,dest,0,arr.length);
// 遍歷數(shù)組
for (int i = 0; i <dest.length ; i++) {
System.out.println(dest[i]);
}
// 2.計算當(dāng)前時間的毫秒數(shù)
System.out.println(System.currentTimeMillis());
// 一般用于計時
long start = System.currentTimeMillis();
for (int i = 0; i <99999999 ; i++) {
for (int j = 0; j <99999999 ; j++) {
int result = i+j;
}
}
long end = System.currentTimeMillis();
System.out.println(end-start);
// 3.System.gc()告訴垃圾回收器進(jìn)行垃圾回收
new Student("aaa",18);
new Student("bbb",17);
new Student("ccc",16);
System.gc();
// 4.退出jvm
System.exit(0);
System.out.println("程序結(jié)束了");
}
}還有一個Student類用來判斷垃圾是否回收:進(jìn)行了封裝,有參構(gòu)造,無參構(gòu)造,重寫toSting方法
和finalize方法。
package com.classes.system;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
protected void finalize() throws Throwable {
System.out.println(name+"被回收了"+"年齡為"+age);
}
}運(yùn)行結(jié)果:

到此這篇關(guān)于Java SimpleDateFormat與System類使用示例詳解的文章就介紹到這了,更多相關(guān)Java SimpleDateFormat與System內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決java編譯錯誤( 程序包javax.servlet不存在javax.servlet.*)
這篇文章主要介紹了解決java編譯錯誤的相關(guān)資料,主要解決 程序包javax.servlet不存在javax.servlet.*的問題,需要的朋友可以參考下2017-08-08
SpringBoot構(gòu)建RESTful API的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot構(gòu)建RESTful API的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
mybatis使用@mapkey獲取的結(jié)果的鍵(key)為null問題
這篇文章主要介紹了mybatis使用@mapkey獲取的結(jié)果的鍵(key)為null問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
Java?Spring?boot實(shí)現(xiàn)生成二維碼
大家好,本篇文章主要講的是Java?Spring?boot實(shí)現(xiàn)生成二維碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02
Spring Security保護(hù)用戶密碼常用方法詳解
這篇文章主要介紹了Spring Security保護(hù)用戶密碼常用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09

