集册 Java实例教程 从日历类中获取日期值

从日历类中获取日期值

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

490
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从日历类中获取日期值

import java.text.DateFormatSymbols;

import java.util.Calendar;
/** 
来 自 
NowJava.com
**/


public class Main {


  public static void main(String[] args) {

    Calendar gCal = Calendar.getInstance();


    // Month is based upon a zero index, January is equal to 0,

    // so we need to add one to the month for it to be in a standard format

    int month = gCal.get(Calendar.MONTH) + 1;

    int day = gCal.get(Calendar.DATE);//N o w  J a v a  .   c o m

    int yr = gCal.get(Calendar.YEAR);


    String dateStr = month + "/" + day + "/" + yr;

    System.out.println(dateStr);


    int dayOfWeek = gCal.get(Calendar.DAY_OF_WEEK);


    // Print out the integer value for the day of the week

    System.out.println(dayOfWeek);


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

    int min = gCal.get(Calendar.MINUTE);

    int sec = gCal.get(Calendar.SECOND);


    System.out.println(hour + ":" + min + ":" + sec);


    // Create new DateFormatSymbols instance to obtain the String value for dates

    DateFormatSymbols symbols = new DateFormatSymbols();

    String[] days = symbols.getWeekdays();

    System.out.println(days[dayOfWeek]);


    // Get crazy with the date!

    int dayOfYear = gCal.get(Calendar.DAY_OF_YEAR);

    System.out.println(dayOfYear);


    
展开阅读全文