1. 程式人生 > >5.8 日期字符串數字的格式化

5.8 日期字符串數字的格式化

date() 向下取整 exti rand int 2.7 highlight pri double

數字的格式化

①Math類:

abs(); 絕對值

public class Test {

	public static void main(String[] args) {
                 int a = Math.abs(-7);
                System.out.println(a);    // 打印7
}  

ceil(); 向上取整

     floor(); 向下取整

public class Test {

	public static void main(String[] args) {
				
		double d1 = Math.ceil(12.345);                                  
		double d2 = Math.ceil(12.745);
		double d3 = Math.floor(12.345);
		double d4 = Math.floor(12.745);
		System.out.println(d1);                          //打印13
		System.out.println(d2);                         //打印13
		System.out.println(d3);                         //打印12
		System.out.println(d4);                         //打印12
         }
}  

round(); 四舍五入取整

public class Test {

	public static void main(String[] args) {
		
		double d5 = Math.round(13.111);
		double d6 = Math.round(13.711);
		System.out.println(d5);                    //打印13       
		System.out.println(d6);                   //打印14
              }
} 

random(); 取隨機數(0-1,不包括1)

還可以用java.util.Random

nextInt(int bounds)

全球唯一標識 UUID 一般用於文件上傳,重名的。上傳時隨機生成改名

import java.util.UUID;
public class Test {

	public static void main(String[] args) {
			
		UUID uuid = UUID.randomUUID();
		System.out.println(uuid);               //打印一個很長的名字
        }
}

   Date 時間戳 某一個時間點到當前時間的毫秒數,弊端:服務器快會重名

public class Test {

	public static void main(String[] args) {
			
		Date date = new Date();
		System.out.println(date.getTime());     //一串數字
	}

}

  

5.8 日期字符串數字的格式化