简体   繁体   中英

Java Calendar considering hours

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM