Home >Java >javaTutorial >Java example - get year, month, etc.

Java example - get year, month, etc.

黄舟
黄舟Original
2017-02-18 09:53:021708browse

The following example demonstrates how to use the Calendar class to output the year, month, etc.:

/*
 author by w3cschool.cc
 文件名:Main.java
 */import java.util.Calendar;public class Main {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        int doy = cal.get(Calendar.DAY_OF_YEAR);

        System.out.println("当期时间: " + cal.getTime());
        System.out.println("日期: " + day);
        System.out.println("月份: " + month);
        System.out.println("年份: " + year);
        System.out.println("一周的第几天: " + dow);  // 星期日为一周的第一天输出为 1,星期一输出为 2,以此类推
        System.out.println("一月中的第几天: " + dom);
        System.out.println("一年的第几天: " + doy);
    }}

The output result of running the above code is:

当期时间: Fri Mar 27 21:44:15 CST 2015
日期: 27
月份: 3
年份: 2015
一周的第几天: 6
一月中的第几天: 27
一年的第几天: 86

The above is the Java example - Get the content of the year, month, etc. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn