3

I am using firebase and building a application with Android.

I have a DatePicker, where I can pick the day of week, hour and minutes.

When I pick the date and press submit, I want to store not the current date, or not even the date selected, but the next date related to the dayOfWeek chosen.

Imagine I choose Thursday, tomorrow is Thursday I want to save that date and the hour chosen by the current user either.

At the moment I tried something like this:

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.DATE, dayOfWeek);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
dateMatch = calendar.getTime();

The hour and minutes are fine, but how can I store the next day/month/year?

Antonio Costa
  • 183
  • 5
  • 15
  • use `add` method on `Calendar` object – Shayan Pourvatan May 08 '17 at 13:16
  • you can add the 7 days in the selected date in calendar, you will get the dayofweek choose in next week – Zakir hussain May 08 '17 at 13:18
  • i used add on the calendar, but i want the next day of week, it can be less then 7 days, for example today is monday, tommorrow is tuesday, if i choose tuesday i want to get the tommorrow date, at the moment when i choose tuesday i get day 11 :S – Antonio Costa May 08 '17 at 13:46

1 Answers1

5

tl;dr

ZonedDateTime.of(
    LocalDate.now( ZoneId.of( "America/Montreal" ) )
             .with( TemporalAdjusters.next( DayOfWeek.THURSDAY ) ) ,
    LocalTime.of( hours , minutes ) ,
    ZoneId.of( "America/Montreal" )
).toString()

The ZonedDateTime class adjusts the time-of-day if invalid for that date in that zone.

java.time

The modern approach uses the java.time classes. Avoid the troublesome old legacy date-time classes seen in the Question.

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

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

Determine the DayOfWeek enum object represented by your UI widget. If tracking by number, that class numbers 1-7 for Monday-Sunday per ISO 8601 standard.

DayOfWeek dow = DayOfWeek.of( 1 ) ;  // Monday=1.

Use a TemporalAdjuster found in TemporalAdjusters to determine the next date with that same day-of-week.

LocalDate ld = today.with( TemporalAdjusters.next( dow ) ) ;

Instantiate a LocalTime from your hours and minutes numbers.

LocalTime lt = LocalTime.of( hours , minutes );

Combine to determine an actual moment in the timeline.

ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;

Your particular time-of-day may not be valid for that date in that zone because of anomalies such as Daylight Saving Time (DST). The ZonedDateTime class adjusts as needed. Be sure to read the class doc to understand its policies in making that adjustment.

You can serialize that object to text using the standard ISO 8601 format, extended by this class to append the name of the time zone in square brackets.

String output = zdt.toString() ;

Reconstitute the object by parsing such strings.

ZonedDateTime zdt = ZonedDateTime.parse( input ) ;

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?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • thank you friend, do you know a way to do that with calendar? – Antonio Costa May 08 '17 at 16:29
  • @AntonioCosta **Avoid using `java.util.Calendar`**. That class is poorly designed, flawed, and confusing – a bloody mess.The java.time classes were developed to replace `Calendar`, `Date`, and such. See the last part of the section I added to my Answer, for using a back-port of java.time in Android. Well worth the bother of adding to your project. – Basil Bourque May 08 '17 at 18:46