如何在Java中显示当月的日期和月份? [英] How to display date of a current month with the month and year in Java?

查看:378
本文介绍了如何在Java中显示当月的日期和月份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中动态地显示特定月份的日期,月份和年份?

How to display the date, month, and year of a particular month in for loop dynamically in Java?

推荐答案

简要介绍了Java中的 SimpleDateFormat GregorianCalendar 类的基础知识。这是根据你的问题做的最好的。

This demonstrates briefly some of the basics of the SimpleDateFormat and GregorianCalendar classes in Java. It was the best I could do based on your question.

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

public class Main {
    public static void main(String[] args) {
        int year = 2012;
        int month = 4;

        /* The format string for how the dates will be printed. */
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

        /* Create a calendar for the first of the month. */
        GregorianCalendar calendar = new GregorianCalendar(year, month, 1);

        /* Loop through the entire month, day by day. */
        while (calendar.get(GregorianCalendar.MONTH) == month) {
            String dateString = format.format(calendar.getTime());
            System.out.println(dateString);

            calendar.add(GregorianCalendar.DATE, 1);
        }
    }
}

这篇关于如何在Java中显示当月的日期和月份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆