How to display date of a current month with the month and year in Java?

前端 未结 2 778
天命终不由人
天命终不由人 2021-01-21 21:19

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

相关标签:
2条回答
  • 2021-01-21 21:34

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-21 21:43

    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.

    0 讨论(0)
提交回复
热议问题