2

How can I add/subtract with time in Java? For example, I'm writing a program that when you input your bedtime, it then adds 90 minutes (the length of 1 sleep cycle) to tell you the ideal wake up time.

Scanner input;
input = new Scanner (System.in);

int wakeup0;
int wakeup1;
int wakeup2;
int wakeup3;
int wakeup4;
int wakeup5;

System.out.println("When will you be going to bed?");
int gotobed = Integer.parseInt(input.nextLine());
wakeup0 = gotobed + 90;
wakeup1 = wakeup0 + 90;
wakeup2 = wakeup1 + 90;
wakeup3 = wakeup2 + 90;
wakeup4 = wakeup3 + 90;
wakeup5 = wakeup4 + 90;

System.out.println("You should set your alarm for: "+wakeup0+" "+wakeup1+" "+wakeup2+" "+wakeup3+" "+wakeup4+" or "+wakeup5);

How do I make it so that when I add 90 to 915 it gives me 1045 rather than 1095?

4

4 回答 4

2

Calculate everything in milliseconds. That might at first seem inconvenient but the math is quite straightforward.

When inputting the bedtime, you need to decide how the user will do this. Will it be hours and minutes. Will there be AM/PM or 24 hour clock. And what time zone will you be considering. All of this is done with a Calendar or DateFormat class and it will give you a single long value with the specified time in it.

After that, adding a time offset is easy.

SimpleDateFormat df = new SimpleDateFormat("HHmm");
long bedttime = df.parse(inputValue).getTime();
long wakeup1 = bedttime + (90 * 60 * 1000);   //90 minutes in milliseconds.
long wakeup2 = wakeup1  + (90 * 60 * 1000);   //90 minutes in milliseconds.
long wakeup3 = wakeup2  + (90 * 60 * 1000);   //90 minutes in milliseconds.
long wakeup4 = wakeup3  + (90 * 60 * 1000);   //90 minutes in milliseconds.
long wakeup5 = wakeup4  + (90 * 60 * 1000);   //90 minutes in milliseconds.

System.out.println("The first wakeup time is "+df.format(new Date(wakeup1)));

Each long value holds the time that the alarm should go off. Use the formatter again to generate a user friendly representation of that time value.

于 2013-10-09T01:51:47.807 回答
0

Firstly, there is no direct conversion from a 4 digit number to a time. So you will need to use Calendar

From this you can get the current date/time Calendar rightNow = Calendar.getInstance();

Then you can use the add method to add your 90 minutes see http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#add(int, int)

于 2013-10-09T01:43:19.397 回答
0

This will be easier if you separate the hours and minutes given that an hour is made up of 60 and not 100 minutes. Ask the user to input their time as "HH:MM" and parse it using split(":") to get the hours and minutes separately:

    System.out.println("When will you be going to bed?");
    String rawBedtime = input.nextLine();
    String[] gotobed = rawBedtime.split(":");
    int minutes = Integer.parseInt(gotobed[0]);
    int hours = Integer.parseInt(gotobed[1]);

To get the number of hours and minutes to add:

    int additionalHours = 90/60;
    int additionalMinutes = 90%60;

However, I would probably Java's Calendar or Date classes. They will take care of a lot of the nitty-gritty for you.

于 2013-10-09T01:44:46.930 回答
0

May not be the exact reply to your question. You should probably use Calendar and SimpleDateFormat in this case like below.

SimpleDateFormat df = new SimpleDateFormat("HHmm");
System.out.println("When will you be going to bed?");
String gotobed = input.nextLine();
Date date = df.parse(gotobed);

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

calendar.add(Calendar.MINUTE, 90);
String wakeup0 = df.format(calendar.getTime());

calendar.add(Calendar.MINUTE, 90);
String wakeup1 = df.format(calendar.getTime());

calendar.add(Calendar.MINUTE, 90);
String wakeup2 = df.format(calendar.getTime());

calendar.add(Calendar.MINUTE, 90);
String wakeup3 = df.format(calendar.getTime());

calendar.add(Calendar.MINUTE, 90);
String wakeup4 = df.format(calendar.getTime());

calendar.add(Calendar.MINUTE, 90);
String wakeup5 = df.format(calendar.getTime());

System.out.println("You should set your alarm for: " + wakeup0 + " "
        + wakeup1 + " " + wakeup2 + " " + wakeup3 + " " + wakeup4
        + " or " + wakeup5);

For Input 0900 the output will be,

When will you be going to bed?
0900
You should set your alarm for: 1030 1200 1330 1500 1630 or 1800
于 2013-10-09T01:49:13.323 回答