0

I'm currently making my first app for android, and an nearly finished. I have run into a bit of an issue, which who knows,I may be over thinking it.

Anyway, the issue is this: I have a date picker, which currently allows you to pick a day from the month and edit some information for a given date.

Now I am trying to select the month and display all the information for the month. Where the problem lies is determining the days of a month.

My months start on the first Sunday of a calendar month and end on the Saturday following the last day of the last month.

Ex:

  • the 1st day of the month is a Saturday, therefore the first day of the month I need is the 2nd.
  • The last day of the month is a Friday, so the last day of my month is Saturday the 1st of the following month.

I have a few ideas as far as what to do, but I feel like I am overcomplicating and underestimating the power of calendars in java. any help or advice would be appreciated.

Thanks

Eduardo Briguenti Vieira
  • 4,351
  • 3
  • 37
  • 49
Jason
  • 1
  • _underestimating the power of calendars in java_ Ha, haha, hahaha. – Sotirios Delimanolis Mar 07 '14 at 22:25
  • They're that bad? So far they've done all that I need, albeit in a very round-about way... – Jason Mar 07 '14 at 22:32
  • Is the question more of knowing how to get the day of the week for any given date? If so, check out this wikipedia articule, it has several algorithms that can be used to determine this. If it is simply a matter of how to display a dialog for the users to select a date, maybe you could look into a library such as this rather than implement your own: https://github.com/derekbrameyer/android-betterpickers/ – cjbrooks12 Mar 07 '14 at 22:37
  • what I'd like to accomplish is determine if the day is the first Sunday & last Saturday of the month. Ecerything i've seen shows working within the calendar month, but never out of or in the way i'm trying. I will take a look at that link! – Jason Mar 07 '14 at 22:43
  • @user3394582 Yes, java.util.Date & .Calendar really are that bad. They are so bad that Java 8 has a new built-in `java.time` package (inspired by Joda-Time) to supplant those old classes. So, go directly to [Joda-Time](http://www.joda.org/joda-time/). Search StackOverflow for many examples. Your question specifically has been answered multiple times. Look at [DateTime](http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html) class, with its `plusDays` and `getDayOfWeek` methods. – Basil Bourque Mar 08 '14 at 04:31

2 Answers2

0

Well, this might be not a correct answer to your actual question but maybe it helps somehow. If you have the possibility you might want to consider using the JodaTime library JodaTime wich has much mor convenient methods for doing such things. For instance just adding a month to any given date, or get the maximum number of days for a given month.

Markus
  • 1,016
  • 1
  • 8
  • 23
0

Using java.time

The modern approach uses the java.time classes.

a given date

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = LocalDate.of( 2016 , Month.JANUARY , 23 );

the first Sunday of a calendar month

The TemporalAdjuster interface provides for classes that manipulate date-time values. But understand that java.time uses immutable objects. Rather than alter the fields of an object (“mutating”), we instantiate a fresh object with values based on those of the original object.

The TemporalAdjusters class (note the plural s) provides handy implementations of TemporalAdjuster. These include getting the first, last, or nth day-of-week of a month. We want first Sunday of the month, so we specify the predefined enum object DayOfWeek.SUNDAY.

LocalDate firstSundayOfMonth = ld.with( TemporalAdjusters.firstInMonth( DayOfWeek.SUNDAY ) );

end on the Saturday following the last day of the last month

We have another TemporalAdjuster to get the last day of the month.

LocalDate lastLocalDateOfMonth = ld.with( TemporalAdjusters.lastDayOfMonth() ) ;

And another adjuster to get the same or next day-of-week.

LocalDate nextOrSameSaturdayOfEndOfMonth = 
    lastLocalDateOfMonth .with( nextOrSame( DayOfWeek.SATURDAY ) ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154