How to display the date, month, and year of a particular month in for loop dynamically in Java?
-
1Is this homework? If so, please tag it as such. – dty Mar 30 '12 at 11:48
-
Also, you're better served showing what you've tried so far and describing a specific problem you've encountered rather than simply trying to get people to hand out free code that solves your problem. – dty Mar 30 '12 at 11:49
-
2what have you tried? http://mattgemmell.com/2008/12/08/what-have-you-tried/ – Bogdan Emil Mariesan Mar 30 '12 at 11:49
-
No. Am trying in a different manner.Please give me suggestions. – nan Mar 30 '12 at 11:49
-
what i tried is divided the sundays and saturdays of a month seperately.And iam trying a simple program. – nan Mar 30 '12 at 11:51
-
In my application,am entering the employee details.And i created a table named as empdet.And also am updating the values to database.One of those fields,one field is date. – nan Mar 30 '12 at 11:53
-
i'm sorry but in order to get an answer you should make the question in [SO] format. – Bogdan Emil Mariesan Mar 30 '12 at 11:54
-
Date is storing in dd-mm-yyyy, format.With that reference to date,i want to display the emp details. – nan Mar 30 '12 at 11:54
-
Check out the java.util.Calendar class: http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html – Raj Iyengar Mar 30 '12 at 11:55
-
1*"what i tried is.."* For better help sooner, post an ***[SSCCE](http://sscce.org/)*** of your best effort, and add a specific question as to how to implement an aspect of the next stage. – Andrew Thompson Mar 30 '12 at 12:03
-
How Can i show only dates of a particular month in dd-mm-yyyy format – nan Mar 30 '12 at 12:04
2 Answers
Using java.time
The other Answer uses the troublesome old date-time classes, now legacy, supplanted by the java.time classes.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
Time zone
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
YearMonth
We care about the whole month. So use a YearMonth
object to represent that.
YearMonth ym = YearMonth.from( today );
Get the first of the month.
LocalDate localDate = ym.atDay( 1 );
Loop, incrementing the date by one day at a time, until past the end of month. We can test that fact by seeing if each incremented date has the same YearMonth
as today. Collect each date in a List
.
List<LocalDate> dates = new ArrayList<>( 31 ); // Collect each date. We know 31 is maximum number of days in any month, so set initial capacity.
while( YearMonth.of( localDate).equals( ym ) ) { // While in the same year-month.
dates.add( localDate ); // Collect each incremented `LocalDate`.
System.out.println( localDate );
// Set up next loop.
localDate = localDate.plusDays( 1 );
}
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 1
- 1

- 303,325
- 100
- 852
- 1,154
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);
}
}
}

- 14,971
- 11
- 66
- 97
-
1FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 08 '19 at 21:40