0

What I want to happen - if the date (that is given from the parameters) is equal to the last day of the month, I want the cost to be increased by 10%. I.e if dayOfTravel = last day of the month

  public static double ReturnJourneyCost(int journeyID, int dayOfTravel, int monthOfTravel, int yearOfTravel){

    Cost = Double.parseDouble(XMLLoad.load(journeyID, 3));
    return Cost;

}
  • What's the issue? I don't an attempt to solve the problem. – Rohit Jain Mar 24 '14 at 16:41
  • On a side note, Java naming conventions recommend method names start with lower case – Averroes Mar 24 '14 at 16:44
  • @Averroes, even for `static` methods? – Ash Burlaczenko Mar 24 '14 at 16:44
  • Worst case add a day, see if the month changed... – Tony Hopkinson Mar 24 '14 at 16:46
  • 1
    @AshBurlaczenko AFAIK yes – Averroes Mar 24 '14 at 16:46
  • I want it to do this - if the last day of the month is equal to dayOfTravel then it adds 10% to the Cost variable. @RohitJain – user3456349 Mar 24 '14 at 16:54
  • possible duplicate of [How to get the first day of the current week and month?](http://stackoverflow.com/questions/2937086/how-to-get-the-first-day-of-the-current-week-and-month). And [this](http://stackoverflow.com/q/4786169/642706). And [this](http://stackoverflow.com/q/8997228/642706). And [this](http://stackoverflow.com/q/9397203/642706). And [this](http://stackoverflow.com/q/19488658/642706). And [this](http://stackoverflow.com/q/13624442/642706). – Basil Bourque Mar 25 '14 at 06:03

4 Answers4

6

Using old JDK-stuff the answer might look like:

public static boolean isLastDayOfMonth(int year, int month, int day) {
  GregorianCalendar gcal = new GregorianCalendar(year, month - 1, day);
  gcal.add(Calendar.DATE, 1);
  return (gcal.get(Calendar.DAY_OF_MONTH) == 1);
}

In Java 8 (better because it avoids any implicit reference to system timezone):

public static boolean isLastDayOfMonth(int year, int month, int day) {
  LocalDate date = LocalDate.of(year, month, day);
  return date.lengthOfMonth() == day;
}
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • The Java 8 solution is very clearly to be preferred. When this answer was written 8 years ago, Java 8 was brand new. Today no one should use `GregorianCalendar`. – Ole V.V. Mar 19 '22 at 10:30
4

Something like this, using GregorianCalendar.getActualMaximum(), will do the trick:

GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.YEAR, yearOfTravel);
calendar.set(Calendar.MONTH, monthOfTravel); // warning: 0-based!
calendar.set(Calendar.DAY_OF_MONTH, dayOfTravel);

int daysInThisMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
if (daysInThisMonth == dayOfTravel)
{
    // increase cost by 10%
}
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

You need to write some custom code...

public boolean isLastDay(Date date) {
    Calendar now = Calendar.getInstance();
    now.setTime(date);
    Calendar c = Calendar.getInstance();
    c.setTime(date);

    c.add(Calendar.DAY_OF_MONTH, 1);
    return c.get(Calendar.MONTH) != now.get(Calendar.MONTH);
}
CMR
  • 986
  • 7
  • 16
0

You can try this:

Calendar calendar = Calendar.getInstance();

if (calendar.get(Calendar.MONTH) == monthOfTravel && 
&& calendar.ger(Calendar.YEAR) == yearOfTravel && calendar.getActualMaximum(Calendar.DAY_OF_MONTH) == dayOfTravel) {
//...
}
Averroes
  • 4,168
  • 6
  • 50
  • 63