- SimpleDateFormat 是java. text包下的。专门负责日期格式化的
2.yyyy年(年是4位)
MM月(月是2位)
dd 日
HH时
mm分
ss 秒
SSS毫秒(毫秒3位,最高999。 1000毫秒代表1秒)
注意:在日期格式中,除了y M d Hm s S这些字符不能随便写之外,剩下的符号格式自己随意組织。
SimpleDateFormat a = new SimpleDateFormat( pattern: “yyyy-MM-dd HH:mm:ss SSS”)
String b= a . format(a);
System.out.println(b) - String --> Date
String a = “2008-08-08 08:08:08 888” ;
SimpleDateFormat b = new SimpleDateFormat( pattern: “yyyy-MM-dd HH:mm:ss SSS”);
Date c = b.parse(a);
System. out . println©;
parse会报错,在psvm里加入throws Exception - long a = System. currentTimeMillis();
System . out . print1n(a); //1583377912981
统计一个方法耗时
在调用目标方法之前记录一个毫秒数
long begin = System. currentTimeMillis();
print();
在执行完目标方法之后记录一个毫秒数
long end= System. currentTimeMillis();
需求:统计一个方法执行所耗费的时长
System. out . println(“耗费时长”+(end - begin)+“毫秒”);
public static void print(){
for(int i = 0; i < 1000; i++){
System. out.println("i =”+ i); - 分配一个 Date对象,并将其初始化为表示自称为“时代”的标准基准时间以后的指定毫秒数,即1970年1月1日00:00:00 GMT。
Date s1 = new Date(1);
SimpleDateFormat a = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss SSS”);
String b = a.format(s1);
System.out.println(b);
输出昨天现在的时间
Date s2 = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
SimpleDateFormat c = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss SSS”);
String d = c.format(s2);
System.out.println(d);
Date
最新推荐文章于 2023-08-20 01:52:18 发布