日期时间类,按特定格式显示日期时间

1.Date类

Date date = new Date(); // 创建现在的日期
long value = date.getTime(); // 获得1970年1月1日00:00:00GMT到现在的毫秒数

2.SimpleDateFormat类

Date date = new Date(); // 创建现在的日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS"); // 创建日期格式,集体格式如下图所示
sdf.format(date); // 改变现在日期的格式

 

3.Calendar类

是一个抽象类,不能通过new实例化,获得对象通过getInstance()方法。

Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTimeInMillis());
System.out.println(calendar.getTime());
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH) + 1);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
System.out.println(calendar.get(Calendar.DAY_OF_WEEK) - 1);

 

posted @ 2019-04-22 21:22  想看云飞却没风~  阅读(300)  评论(0编辑  收藏  举报