0

Hy guys!
I'm writing a software to keep track of room bookings. Each room has a day of booking, a start time, an end time. The problem is I may have a booking in between two day (eg. from 18-02-2015 23:00 to 19-02-2015).
How can I automate this increasing process without asking the user to insert an end date?

I'm using Calendar for the date but for hours and minutes I just take values from two TextFields.

Zhedar
  • 3,480
  • 1
  • 21
  • 44
  • 1
    How would the user specifiy a time spam without setting an end date? – Zhedar Feb 18 '15 at 22:08
  • Evidently the property set (day, start time, end time) does not suit your needs as well as you could wish. Perhaps (start day, start time, duration) would fit the problem better. Not only does that avoid issues with reservations straddling two dates, it can even accommodate multi-day bookings. – John Bollinger Feb 18 '15 at 22:20
  • So this is a rent-by-the-hour No-Tell Motel? – Basil Bourque Feb 18 '15 at 22:24
  • Thanks John. I have also a duration variable, in order to calculate the amount the customers should pay. Since is an hours based service (it's a rehersal room) I don't really need the users typing an end date, but I have to manage to occasion of a booking between two days. Even listening to yours advice I have an idea: I should create a startCalendar and an endCalendar. So, if the endTime its between 00 and 04 lets increase endCalendar by one day. Then, I can calculate the difference between the two Calendars and make my math. But: which functions i should use? I miss the code. – Marco Silvestri Feb 19 '15 at 07:45

2 Answers2

0

You could specify the amout of days a room is booked. Then you just have to add the number of days to your first Calendar object.
Working example how to add days in a simple way:

int days = 2; //default duration, placeholder
Calendar now = Calendar.getInstance();
Calendar end = (Calendar) now.clone();
    end.add(Calendar.DATE, days);

The Calendar end would then be set the current time in two days.

Normally I wouldn't recommend using Object.clone(), but this answer says it is safe to do so.

There is no way to project a time span with the old Calendar-API (with just one Calendar). But if you could use the new Java 8 Date and Time API, you may use Periods und Durations. They may come in useful, if you need to determine durations for bookings.
However, I can just recommend you to look into the API, since I haven't worked with it enough to provide a useful example.

Community
  • 1
  • 1
Zhedar
  • 3,480
  • 1
  • 21
  • 44
0

tl;dr

ZonedDateTime.of( 2015 , Month.FEBRUARY , 18 , 23 , 0 , 0 , 0 , ZoneId.of( "Europe/Paris" ) )
             .plus( Duration.ofHours( 3 ) )

2015-02-19T02:00:00+01:00[Europe/Paris]

java.time

The accepted answer uses the outmoded old legacy date-time classes that have proven to be so troublesome and confusing. Now supplanted by the java.time classes.

To specify a moment on the timeline, include the time zone to give context to your date-and-time.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime start = ZonedDateTime.of( 2015 , Month.FEBRUARY , 18 , 23 , 0 , 0 , 0 , z );

Track your reserved number of hours as a Duration.

Duration duration = Duration.ofHours( 3 );

The ZonedDateTime class knows how to do math with a Duration.

ZonedDateTime zdtStop = zdtStart.plus( duration );

You say you have two data-entry fields for hours and minutes. Convert each text entry to a number. The Long class parses a string to a long primitive.

From those numbers get a Duration of hours and minutes.

Duration duration = Duration.ofHours( Long.parseLong( hoursEntry ) )
                            .plusMinutes( Long.parseLong( minutesEntry ) ) ;

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