How is Joda or Java true if the day is the first day of the month?

How can I write a method in Joda or Java that returns a boolean if today is the first day of the current month ? For example, if today the date is the first day of this month, it will return true.

It could be something like this:

public static boolean isFirstDayOfTheMonth( Date dateToday ){
boolean isFirstDay = false;
//code to check if dateToday is first day of the current month.
returns isFirstDay;
}

Thank you in advance!

0
source share
5 answers

LocalDate::getDayOfMonth

With Java SE 8 and later, call LocalDate::getDayOfMonth.

public static boolean isFirstDayOfTheMonth(LocalDate dateToday ){
  return dateToday.getDayOfMonth() == 1;
}
+4
source

Using Joda-time

public static boolean isFirstDayOfTheMonth( DateTime dateToday ){
returns dateToday.dayOfMonth().get()==1
}
+2
source

.

public static boolean isFirstDayOfTheMonth(Date dateToday){
  Calendar c = new GregorianCalendar();
  c.setTime(dateToday );
  if (c.get(Calendar.DAY_OF_MONTH) == 1) {
    return true;
  }
  returns false;
}
0

Use this with regular java Date:

public static boolean isFirstDayOfTheMonth( Date dateToday ){
     if(dateToday.getDate() == 1){
        System.out.println("First of the month");
        return true;
    }else{
        return false';
    }

}

Hope this helps. And good luck with the rest of your code.

0
source

Do not use the class Date, it has deprecated methods. The calendar is better.

public boolean isFirstDayOfMonth(Calendar calendar)
{
    return calendar.get(Calendar.DAY_OF_MONTH) == 1;
}

Example method call:

isFirstDayOfMonth(Calendar.getInstance());
0
source

All Articles