芝麻_糊

导航

java中Calendar类

1.测试代码:

 1 package com;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Calendar;
 5 import java.util.Date;
 6 
 7 /**
 8  * Created by huyanxia on 2017/9/30.
 9  */
10 public class calendarTest {
11     public static void main(String[] args) {
12         Calendar calendar = Calendar.getInstance();
13         Date date = new Date();
14         calendar.setTime(date);
15         int day = calendar.get(Calendar.DAY_OF_YEAR);
16         int month = calendar.get(Calendar.DAY_OF_MONTH);
17         int second = calendar.get(Calendar.SECOND);
18         int maxdays = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
19         int mindays = calendar.getActualMinimum(Calendar.DAY_OF_YEAR);
20         int maxmonths = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
21         int minmonths = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
22         //int day = calendar.get(Calendar.DAY_OF_YEAR);
23         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd-HH-mm-ss");
24         System.out.println("现在时间"+simpleDateFormat.format(date));
25         System.out.println("今天是2017年的第几天:"+day);
26         System.out.println("今天是2017年的9月第几天:"+month);
27         System.out.println("当前的秒数:"+second);
28         System.out.println("今年天数最大值:"+maxdays);
29         System.out.println("今年天数最小值:"+mindays);
30         System.out.println("月份最大值:"+maxmonths);
31         System.out.println("月份最小值:"+minmonths);
32     }
33 }

结果:

注意:格式化时,年月日时分秒:“YYYY-MM-dd-HH-mm-ss”(24小时制)

“YYYY-MM-dd-hh-mm-ss”(12小时制)

 

posted on 2017-09-30 11:36  芝麻_糊  阅读(302)  评论(0编辑  收藏  举报