集册 Java实例教程 使用日历获取当前日期时间值

使用日历获取当前日期时间值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

544
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用日历获取当前日期时间值

 /* 来自 nowjava - 时  代  Java*/

import java.util.Calendar;

 

public class Main {

 

  public static void main(String[] args) {

    Calendar now = Calendar.getInstance();

   

    //get current date, year and month

    System.out.println("Current Year is : " + now.get(Calendar.YEAR));

    //month start from 0 to 11

    System.out.println("Current Month is : " + (now.get(Calendar.MONTH) + 1 ));

    System.out.println("Current Date is : " + now.get(Calendar.DATE));

 

    //get current time information

    System.out.println("Current Hour in 12 hour format is : "                        

                    + now.get(Calendar.HOUR));
                    /*
                    n o w  j a v a  . c o m 提供
                    */

    System.out.println("Current Hour in 24 hour format is : "

                    + now.get(Calendar.HOUR_OF_DAY));

    System.out.println("Current Minute is : " + now.get(Calendar.MINUTE));

    System.out.println("Current Second is : " + now.get(Calendar.SECOND));

    System.out.println("Current Millisecond is : " + now.get(Calendar.MILLISECOND));

   

    //display full date time

    System.out.println("Current full date time is : " +

                (now.get(Calendar.MONTH) + 1)

                + "-"

                + now.get(Calendar.DATE)

                + "-"

                + now.get(Calendar.YEAR)

                + " "

            
展开阅读全文