import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo{
public static void main(String[] args) {
/*系統當前時間*/
Date date = new Date();
System.out.println(date);
/*SimpleDateFormat專門負責日期格式化。
* yyyy 年(年是4位
* MM 月(月是2位)
* dd 日
* HH 時
* mm 分
* ss 秒
* SSS 毫秒(毫秒3位,最高999。1000毫秒代表一秒)
* 注意:在日期格式中,
* 除了y M d H m s S這些字符不能隨便寫之外,
* 剩下的符號格式自己隨意組織。*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
//SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
//SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
String nowTimeStr = sdf.format(date);
System.out.println(nowTimeStr);
}
}
- 日期字符串轉Date類型
1、Date parse(String source)
從給定字符串的開始解析文本以生成日期。
2、代碼示例:
import java.text.SimpleDateFormat;
import java.util.Date;
public class DemoTest{
public static void main(String[] args) throws Exception{
String time = "2020-04-07 22:55:11 745";
//格式要一致
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
Date dateTime = sdf.parse(time);
System.out.println(dateTime);
}
}
public class Demo{
public static void main(String[] args) {
/*獲取自1970年1月1日00:00:00 000到當前系統時間的總毫秒數。*/
long nowTimeNillis = System.currentTimeMillis();
System.out.println(nowTimeNillis);
/*統計一個方法耗時:
* 調用目標方法之前記錄一個毫秒數
* 在執行完目標方法之后記錄一個毫秒數*/
long begin = System.currentTimeMillis();
print();
long end = System.currentTimeMillis();
System.out.println("耗費時長:" + (end-begin) + "毫秒");
}
private static void print(){
for (int i = 0; i < 1000; i++) {
System.out.println("i = " + i);
}
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo{
public static void main(String[] args) {
/*這個時間是1970-01-01 00:00:00 001
* 參數是1毫秒(起點)*/
Date time = new Date(1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss SSS");
String strTime = sdf.format(time);
/*北京是東八區,差八個小時*/
System.out.println(strTime);//1970-01-01 08:00 001
/*獲取昨天的此時的時間*/
Date time2 = new Date(System.currentTimeMillis() - 1000*60*60*24);
String strTime2 = sdf.format(time2);
System.out.println(strTime2);
}
}
import java.text.DecimalFormat;
public class Demo{
public static void main(String[] args) {
/*專門負責數字格式化。
* 數字格式有哪些?
* # 代表任意數字
* ,代表千分位
* . 代表小數點
* 0 代表不夠時補0*/
DecimalFormat df = new DecimalFormat("###,###.##");
String s = df.format(1234.567);
System.out.println(s);//1,234.57
DecimalFormat df2 = new DecimalFormat("###,###.0000");
String s2 = df2.format(1234.567);
System.out.println(s2);//1,234.5670
}
}
- 高精度BigDecimal
BigDecimal屬於大數據,精度極高。不屬於基本數據類型,屬於java對象(引用數據類型)這是SUN提供的一個類。專門用在財務軟件當中。
代碼示例:
import java.math.BigDecimal;
public class Demo{
public static void main(String[] args) {
BigDecimal v1 = new BigDecimal(832.2112);
BigDecimal v2 = new BigDecimal(32.1234);
BigDecimal v3 = v1.multiply(v2) ;
System.out.println(v3);
//輸出:26733.4532620799959863
// 378899171949698136469211733
// 480099563164955611682671587
// 9142284393310546875
}
}
import java.util.Random;
public class Demo{
public static void main(String[] args) {
/*創建隨機數對象*/
Random random = new Random();
/*隨機產生一個int類型取值范圍內的數字。*/
int num = random.nextInt();
System.out.println(num);
/*產生[0~100]之間的隨機數。
* 不能產生101
* nextInt翻譯為:下一個int類型的數據是101,表示只能取到100。*/
int num2 = random.nextInt(101);
System.out.println(num2);
}
}
import java.util.Random;
public class Demo{
public static void main(String[] args) {
Random random = new Random();
int[] a = new int[5];
for (int s = 0; s < a.length; s++) {
a[s] = -1;
}
int index = 0;
while (index < a.length){
int num = random.nextInt(21);
if (judge(a, num)){
a[index++] = num;
}
}
for (int k = 0; k < a.length; k++) {
System.out.println(a[k]);
}
}
public static boolean judge(int[] arr, int key){
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key){
return false;
}
}
return true;
}
}