JAVA 日期与数字的格式化

一.全球唯一标识

System.out.println(UUID.randomUUID());
//随机打印一个不会重复的标识,例:8808fd0f-beeb-433f-91a2-9fce6f2b5ba0

获取时间戳

Date date1 = new Date();
System.out.println(date1.getTime());
//返回某个时间到当前时间的毫秒数,例:1525968089069

 二.数字的格式化

1.类的关系:java.lang.Object——java.text.Format——java.text.NumberFormat——java.text.DecimalFormat(小数格式化)
2.举例:
double a = 12.345678;
DecimalFormat df = new DecimalFormat("00000.##%");
System.out.println(df.format(a));    //显示结果 01234.57%

引用API文档

3.String 用法
double b = 12.3456;
String s = String.format("%.2f", b);
System.out.println(s);       //输出结果12.35
 

4.printf用法

double b = 12.3456;
System.out.println("%.2f", b);    //输出结果为12.35

 三.日期的格式化

1.DateFormat用法

例:

Date date1 = new Date();    //获取当前的系统时间
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); 
DateFormat df1 = DateFormat.getInstance();
String s = df.format(date1);
String s1 = df1.format(date1);
System.out.println(s);    //打印结果2018年5月11日 星期五       
System.out.println(s1);   //打印结果18-5-11 上午1:02

2.SimpleDateFormat用法(DateFormat的子类)

引用API文档

 例:

Date date1 = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss.SSSZ");
String s3 = sdf.format(date1);    
System.out.println(s3);//打印结果2018年05月11日 01:05:22.698+0800


猜你喜欢

转载自www.cnblogs.com/wyc1991/p/9022537.html
今日推荐