Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java last day of month problem

I'm trying to create a date range to cover a full month, i.e.

[startDate; endDate]

As such, I have a reference date and try to create a new date from it. I'm having problem with the "endDate" because I want it to be near the end of the day (i.e. 23:59:59).

The code I'm using is the following:

  public static Date previousMonthLastDate(Date referenceDate) {
    Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.setTime(referenceDate);
    calendar.add(Calendar.MONTH, -1); // move to the previous month
    int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    calendar.set(Calendar.DAY_OF_MONTH, lastDay);
    // set the time to be the end of the day
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);

    return calendar.getTime();
  }

This code is working as expected on the Android emulator. However, running it on a real phone gives the wrong date. As such, I'm assuming it is some kind of timezone problem.

On the phone, instead of giving say 31/August/2010, it gives 01/September/2010. This value seams to be set after the line of code that sets the HOUR_OF_DAY to 23.

Any ideas?

Thanks.

like image 324
MyName Avatar asked Sep 08 '10 21:09

MyName


People also ask

How do you check is current date is last day of the month in Java?

Use the getActualMaximum() method to get the last day of the month. int res = cal. getActualMaximum(Calendar. DATE);

How do I find the last day of the month LocalDate?

Ways to get last day of month in javaCreate new LocalDate object either by using LocalDate. now() or using given date. Get Month object by calling LocalDate's getMonth() method. Use Month's length() method get number of days in the month.

How do you get amonth in java?

The getMonth() method of YearMonth class in Java is used to get the month field from this YearMonth instance with which it is used. It returns the month field as a Month instance. Parameter: This method does not accepts any parameter. Return Value: It returns the month field from this YearMonth instance.

How do I add 7 days to Java Util date?

Adding Days to the given Date using Calendar class Add the given date to the calendar by using setTime() method of calendar class. Use the add() method of the calendar class to add days to the date. The add method() takes two parameter, i.e., calendar field and amount of time that needs to be added.


2 Answers

I can't answer why it's happening, but have you tried setting it to the first day of the next month and subtracting one second/millisecond?

calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.MILLISECOND, -1);
like image 186
waxwing Avatar answered Sep 22 '22 10:09

waxwing


I working in something like that:

With this code, I set the day in interval:

Date day = new Date()

With this code, I get interval:

Calendar startDate = Calendar.getInstance();
Calendar endDate = Calendar.getInstance();

// Set time
startDate.setTime(day);
endDate.setTime(day);

startDate.set(Calendar.DAY_OF_MONTH, 1);
startDate.set(Calendar.HOUR_OF_DAY, 0);
startDate.set(Calendar.MINUTE, 0);
startDate.set(Calendar.SECOND, 0);
startDate.set(Calendar.MILLISECOND, 0);

endDate.set(Calendar.DAY_OF_MONTH, endDate.getActualMaximum(Calendar.DAY_OF_MONTH));
endDate.set(Calendar.HOUR_OF_DAY, 23);
endDate.set(Calendar.MINUTE, 59);
endDate.set(Calendar.SECOND, 59);
like image 30
Eduardo Cuomo Avatar answered Sep 20 '22 10:09

Eduardo Cuomo