Java 中的日期和时间

1.如何取得年 月日、小时分钟秒?

public static void main(String[] args) {
            Calendar cal = Calendar.getInstance();
            System.out.println(cal.get(Calendar.YEAR));
            System.out.println(cal.get(Calendar.MONTH)); // 0 - 11
            System.out.println(cal.get(Calendar.DATE));
            System.out.println(cal.get(Calendar.HOUR_OF_DAY));
            System.out.println(cal.get(Calendar.MINUTE));
            System.out.println(cal.get(Calendar.SECOND));
            // Java 8
            LocalDateTime dt = LocalDateTime.now();
            System.out.println(dt.getYear());
            System.out.println(dt.getMonthValue()); // 1 - 12
            System.out.println(dt.getDayOfMonth());
            System.out.println(dt.getHour());
            System.out.println(dt.getMinute());
            System.out.println(dt.getSecond());
        }

 

posted @ 2019-05-22 14:31  农名工进城  阅读(143)  评论(0编辑  收藏  举报