Question:

Using Booleans in Java?? (10 points)

by  |  earlier

0 LIKES UnLike

Let's say you have a boolean called Morning, which tells you if a particular time is morning or night (AM or PM). And then you want to program something so that it tells you whether or not time1 is earlier than the time2.

For example: I want to say: if the boolean Morning is true for time1 and false for time2, then time1 is earlier than time2. How would i use the boolean? because there are two variables that I need to consider.

Thanks for all your help! :D

 Tags:

   Report

4 ANSWERS


  1. It's not completely clear to me as to who is supposed to own the boolean Morning.  But let me assume you need to A) determine whether a date/time is AM or PM, and B) if one date/time is before another.  This can be easily accomplished using the java.util.Date and java.util.Calendar classes.

    Here, I will assume that each date/time is represented by a Date object, such as below:

    Date time1 = ...;

    Date time2 = ...;//come from somewhere else...

    //determine if AM or PM

    Calendar cal = Calendar.getInstance();

    cal.setTime(time1);

    if (Calendar.AM == cal.get(Calendar.AM_PM)){

    //do something, it's morning!

    }

    //determine if one date before another

    if (time1.before(time2)){

    //do something, time1 comes before time2!

    }

    You can, of course, use them in combination as desired.


  2. if (time1.Morning && !time2.Morning) {

    /* if time1 and time2 are the same day this means something */

    }

  3. webdesign tool-

    http://intelliwebtool.com/

  4. Java tutorial-

    http://codingjava.info/

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.