Java get the first date and last date of a given month and a given year

I am trying to get the first date and last date of this month and year. I used the following code to get the latest date in yyyyMMdd format. But could not get this format. In addition, I want the start date to be in the same format. I am still working on it. Can someone help me in fixing the code below.

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); } 
+10
source share
9 answers

To get a start date

  GregorianCalendar gc = new GregorianCalendar(year, month-1, 1); java.util.Date monthEndDate = new java.util.Date(gc.getTime().getTime()); System.out.println(monthEndDate); 

( Note : in the start date of the day = 1)

for formatted

 SimpleDateFormat format = new SimpleDateFormat(/////////add your format here); System.out.println("Calculated month end date : " + format.format(calculatedDate)); 
+5
source

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

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

YearMonth class named YearMonth represents the month of the year without any specific day or time. From there we can ask about 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 supersede the nasty old obsolete date and time classes, such as java.util.Date , Calendar , and SimpleDateFormat .

The Joda-Time project, currently in maintenance mode , recommends switching to the java.time classes.

To learn more, see the Oracle Tutorial . And a search for many examples and explanations. JSR 310 specification .

You can exchange java.time objects directly with your database. Use a JDBC driver that conforms to JDBC 4.2 or later. No need for strings, no need for java.sql.* .

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a testing ground for possible future additions to java.time. Here you can find some useful classes such as Interval , YearWeek , YearQuarter and others .

+13
source
 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); format.format(calculatedDate); System.out.println("Calculated month end date : " + calculatedDate); 

Change it to

 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String formattedDate = format.format(calculatedDate); System.out.println("Calculated month end date : " + formattedDate); 

More details ...

http://docs.oracle.com/javase/1.5.0/docs/api/java/text/DateFormat.html#format(java.util.Date )

Another approach

 package com.shashi.mpoole; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DateMagic { public static String PATTERN = "yyyyMMdd"; static class Measure { private int month; private int year; private Calendar calendar; public Measure() { // TODO Auto-generated constructor stub init(); } private void init() { // TODO Auto-generated method stub calendar = Calendar.getInstance(); calendar.set(year, month, 1); } public String min() { return format(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); } public String max() { return format(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); } private Date date(GregorianCalendar c) { return new java.util.Date(c.getTime().getTime()); } private GregorianCalendar gc(int day) { return new GregorianCalendar(year, month, day); } private String format(int day) { SimpleDateFormat format = new SimpleDateFormat(PATTERN); return format.format(date(gc(day))); } public Measure month(int month) { // TODO Auto-generated method stub this.month = month -1 ; return this; } public Measure year(int year) { // TODO Auto-generated method stub this.year = year; return this; } } public static void main(String[] args) { Measure measure = new Measure().month(3).year(2076); System.out.println(measure.max()); System.out.println(measure.min()); } } 
+5
source

You can simply use the Calendar class. you should assign the monthly variable in which you want

  Calendar gc = new GregorianCalendar(); gc.set(Calendar.MONTH, month); gc.set(Calendar.DAY_OF_MONTH, 1); Date monthStart = gc.getTime(); gc.add(Calendar.MONTH, 1); gc.add(Calendar.DAY_OF_MONTH, -1); Date monthEnd = gc.getTime(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); System.out.println("Calculated month start date : " + format.format(monthStart)); System.out.println("Calculated month end date : " + format.format(monthEnd)); 
+5
source

Try entering the code for the last day of the month: -

 Calendar c = Calendar.getInstance(); c.set(2012,3,1); //------> c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); System.out.println(sdf.format(c.getTime())); 

http://www.coderanch.com/t/385759/java/java/date-date-month

+4
source

First day:

 Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH); 

Last day of the month:

 Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH); 
+4
source
  GregorianCalendar gc = new GregorianCalendar(year, selectedMonth-1, 1); java.util.Date monthStartDate = new java.util.Date(gc.getTime().getTime()); Calendar calendar = Calendar.getInstance(); calendar.setTime(monthStartDate); calendar.add(calendar.MONTH, 1); calendar.add(calendar.DAY_OF_MONTH, -1); java.util.Date monthEndDate = new java.util.Date(calendar.getTime()) 
+2
source

Although not quite the answer to the OP question, below the methods will give the current month the first and last dates as instances of Java 8+ LocalDate.

 public static LocalDate getCurrentMonthFirstDate() { return LocalDate.ofEpochDay(System.currentTimeMillis() / (24 * 60 * 60 * 1000) ).withDayOfMonth(1); } public static LocalDate getCurrentMonthLastDate() { return LocalDate.ofEpochDay(System.currentTimeMillis() / (24 * 60 * 60 * 1000) ).plusMonths(1).withDayOfMonth(1).minusDays(1); } 

Note: using LocalDate.ofEpochDay(...) instead of LocalDate.now() gives significantly improved performance. Also, using the expression millis-in-a-day instead of the final value, which 86400000 works better. Initially, I thought the latter would work better than the expression: P

Why this answer : Nevertheless, this is not the correct answer to the OP question, but I still answer here when Google showed this question, when I searched for "java 8 get date date date date" :)

+1
source
 public static Date[] getMonthInterval(Date data) throws Exception { Date[] dates = new Date[2]; Calendar start = Calendar.getInstance(); Calendar end = Calendar.getInstance(); start.setTime(data); start.set(Calendar.DAY_OF_MONTH, start.getActualMinimum(Calendar.DAY_OF_MONTH)); start.set(Calendar.HOUR_OF_DAY, 0); start.set(Calendar.MINUTE, 0); start.set(Calendar.SECOND, 0); end.setTime(data); end.set(Calendar.DAY_OF_MONTH, end.getActualMaximum(Calendar.DAY_OF_MONTH)); end.set(Calendar.HOUR_OF_DAY, 23); end.set(Calendar.MINUTE, 59); end.set(Calendar.SECOND, 59); //System.out.println("start "+ start.getTime()); //System.out.println("end "+ end.getTime()); dates[0] = start.getTime(); dates[1] = end.getTime(); return dates; } 
0
source

All Articles