1. 程式人生 > >Java 日期類 Calendar

Java 日期類 Calendar

package com.xuyigang1234.chp05.sec01;
import java.util.Calendar;;
public class TestCalendar {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println("現在是:"+cal.get(Calendar.YEAR)+"年"
                +(cal.get(Calendar.MONTH)+1)+"月"
                +cal.get(Calendar.DAY_OF_MONTH)+"日"
                +cal.get(Calendar.HOUR_OF_DAY)+"時"
                +cal.get(Calendar.MINUTE)+"分"
                +cal.get(Calendar.SECOND)+"秒");
    
//今天星期幾 System.out.println(cal.get(Calendar.DAY_OF_WEEK)); System.out.println(cal.get(Calendar.DAY_OF_MONTH)); String[] weekDays = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; System.out.println("今天是:"+weekDays[cal.get(Calendar.DAY_OF_WEEK)-1]); } }
現在是:2018年11月22日21時43分34秒
5
22
今天是:星期四