1. 程式人生 > >java中Calendar類

java中Calendar類

width system pub 格式 http public led ear 時分秒

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小時制)

java中Calendar類