Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java get the first date and last date of given month and given year [duplicate]

I am trying to get the first date and the last date of the given month and year. I used the following code to get the last date in the format yyyyMMdd. But couldnot get this format. Also then I want the start date in the same format. I am still working on this. Can anyone help me in fixing the below code.

public static java.util.Date calculateMonthEndDate(int month, int year) {
    int[] daysInAMonth = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int day = daysInAMonth[month];
    boolean isLeapYear = new GregorianCalendar().isLeapYear(year);

    if (isLeapYear && month == 2) {
        day++;
    }
    GregorianCalendar gc = new GregorianCalendar(year, month - 1, day);
    java.util.Date monthEndDate = new java.util.Date(gc.getTime().getTime());
    return monthEndDate;
}

public static void main(String[] args) {
    int month = 3;
    int year = 2076;
    final java.util.Date calculatedDate = calculateMonthEndDate(month, year);
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    format.format(calculatedDate);
    System.out.println("Calculated month end date : " + calculatedDate);
} 
like image 246
user1514499 Avatar asked Jan 23 '13 08:01

user1514499


1 Answers

java.time.YearMonth methods ::atDay & ::atEndOfMonth

The new java.time framework in Java 8 (Tutorial) has commands for this.

The aptly-named YearMonth class represents a month of a year, without any specific day or time. From there we can ask for the first and days of the month.

YearMonth yearMonth = YearMonth.of( 2015, 1 );  // January of 2015.
LocalDate firstOfMonth = yearMonth.atDay( 1 );
LocalDate last = yearMonth.atEndOfMonth();

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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, 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 Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). 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.

like image 104
Basil Bourque Avatar answered Sep 21 '22 02:09

Basil Bourque