Java 获取当前时间的三种方法

时间:2025-04-02 20:36:54

1.通过Util包中的Date获取

Date date = new Date(); SimpleDateFormat dateFormat= new
SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
System.out.println(dateFormat.format(date));

2.通过Util包的Calendar 获取

Calendar calendar= Calendar.getInstance(); SimpleDateFormat
dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
System.out.println(dateFormat.format(calendar.getTime()));

3.通过Util包的Calendar 获取时间,分别获取年月日时分秒

Calendar cal=Calendar.getInstance();       
int y=cal.get(Calendar.YEAR);       int m=cal.get(Calendar.MONTH);      
int d=cal.get(Calendar.DATE);       int
h=cal.get(Calendar.HOUR_OF_DAY);       int
mi=cal.get(Calendar.MINUTE);       int s=cal.get(Calendar.SECOND);

顺便说一下,国内又一款开源软件Wall,搭建特别简单,可以搭建个人照片墙和视频墙,有兴趣可以看下教程:/u014641168/article/details/129396364