0

How in Joda or Java can I write a method that returns a boolean value if today is the first day of the current month? Like if today's 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!

Knbm
  • 19
  • 2
  • 6
  • 1
    What have you tried to fill in the `//code to check if dateToday is first day of the month.` part? –  Jun 27 '14 at 12:05
  • 1
    I won't type an entire example but I can tell you that "Calendar" is the way to go. – nablex Jun 27 '14 at 12:06
  • First day of month in which time zone? It's always 5 o'clock somewhere... – Brett Okken Jun 27 '14 at 12:10
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 20 '18 at 05:14

5 Answers5

4

LocalDate::getDayOfMonth

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

public static boolean isFirstDayOfTheMonth(LocalDate dateToday ){
  return dateToday.getDayOfMonth() == 1;
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Puce
  • 37,247
  • 13
  • 80
  • 152
2

Using Joda-time

public static boolean isFirstDayOfTheMonth( DateTime dateToday ){
returns dateToday.dayOfMonth().get()==1
}
Abhishek Mishra
  • 611
  • 4
  • 11
  • Shorten to `dateToday.getDayOfMonth() == 1`. – Basil Bourque Jun 28 '14 at 07:05
  • FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [*java.time*](http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 20 '18 at 05:14
0

This code gives you the result you need.

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;
}
Jens
  • 67,715
  • 15
  • 98
  • 113
0

Use this with a normal java Date:

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

}

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

Roan
  • 1,200
  • 2
  • 19
  • 32
0

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

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

Example method call:

isFirstDayOfMonth(Calendar.getInstance());
Bully WiiPlaza
  • 462
  • 1
  • 6
  • 16