集册 Java实例教程 使用GregorianCalendar类

使用GregorianCalendar类

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

510
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用GregorianCalendar类

import java.util.Calendar;/*来自 N o w J a v a . c o m - 时  代  Java*/

import java.util.Date;

import java.util.GregorianCalendar;


public class Main {

  public static void main(String[] args) {

    GregorianCalendar gc = new GregorianCalendar();

    System.out.println("Current Date: " + getStr(gc));


    // Add 1 year 

    gc.add(Calendar.YEAR, 1);        

    System.out.println("After adding a year: " + getStr(gc));


    // Add 15 days

    gc.add(Calendar.DATE, 15);        

    System.out.println("After adding 15 days: " + getStr(gc));


    long millis = gc.getTimeInMillis();

    Date dt = gc.getTime();

    System.out.println("Time in millis: " + millis);/* 来 自 时 代      J a v a   公   众 号 - nowjava.com*/

    System.out.println("Time as Date: " + dt);

  }

    

  public static String getStr(GregorianCalendar gc) {

    int day = gc.get(Calendar.DAY_OF_MONTH);

    int month = gc.get(Calendar.MONTH);

    int year = gc.get(Calendar.YEAR);

    int hour = gc.get(Calendar.HOUR);

    int minute = gc.get(Calendar.MINUTE);

    int second = gc.
展开阅读全文