sgrgmngt
sgrgmngt

Reputation: 917

Want to get the start and end date of a month from a particular date

I m having a code which gives date of a particular event like :

String date=date1.substring(3,5)+"/"+date1.substring(0,2)+"/"+date1.substring(6,10);

I want the start date and end date of the month of which the above date belongs to..

Kindly help.

Upvotes: 4

Views: 9469

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 338516

tl;dr

YearMonth.from (
    LocalDate.parse(
        input ;
        DateTimeFormatter.ofPattern( "MM/dd/uuuu" )
    )
)
.atDay( 1 )  // & .atEndOfMonth() 

java.time

String date=date1.substring(3,5)+"/"+date1.substring(0,2)+"/"+date1.substring(6,10);

Rather than manipulate the input string, define a formatting pattern to match. Use DateTimeFormatter class.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu" ) ;

Parse your input. Use LocalDate for a date-only value.

LocalDate ld = LocalDate.parse( "01/23/2025" );

Get the YearMonth object for that date’s month.

YearMonth ym = YearMonth.from( ld ) ;

Interrogate for its first day.

LocalDate first = ym.atDay( 1 ); 
LocalDate last = ym.atEndOfMonth() ;

Upvotes: 0

chahuistle
chahuistle

Reputation: 2645

you should consider using java.util.Calendar. You provide a java.util.Date, then you can get all fields. A field would be YEAR, DAY

EDIT (for comment)

I'll assume that in your code snippet date1 is a string... it also seems that you know the format (mm/dd/yyyy or dd/mm/yyyy), so first you need a SimpleDateFormat:

final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); // or dd/MM/yyyy

Then you'll need a way to parse the date... parsing a date means to convert from a string to a date object:

final Date date = dateFormat.parse(date1); // date1 is a date in a string in the given format

You have a Datenow... All you need is to extract information from it using a java.util.Calendar

final Calendar calendar = new Calendar();
calendar.setTime(date);

With the calendar, you can get any date-related information. The first day of the month is trivial... any month starts with day number one... But for the last day of the month you need to get the maximum value for the DAY_OF_MONTH field:

int lastDayOfTheMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

Anyway, I'm not making any of this stuff up... Feel free to browse the Calendar javadoc and the SimpleDateFormat javadoc. It's all there, and I'm sure it is better explained than by me.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500485

I would personally recommend using Joda Time for pretty much all date and time work in Java.

First I'd parse the original value into a LocalDate, then:

// Parse using DateTimeFormatter.parseDateTime().toLocalDate();
LocalDate original = ...; 
LocalDate startOfMonth = original.dayOfMonth().withMinimumValue();
LocalDate endOfMonth = original.dayOfMonth().withMaximumValue();

(For the start of the month, you could just use original.withDayOfMonth(1) of course.)

Upvotes: 4

Jigar Joshi
Jigar Joshi

Reputation: 240900

Use Calendar.getActualMaximum()

To get the max date of the month

cal.getActualMaximum(Calendar.DAY_OF_MONTH)

and for minimum

cal.getActualMinimum(Calendar.DAY_OF_MONTH)

Note : cal is an instance of Calendar

Upvotes: 4

Related Questions