在Java中与时间交互

时间:2013-10-09 01:36:13

标签: java math time

如何在Java中添加/减去时间?例如,我正在编写一个程序,当您输入睡前时间时,它会增加90分钟(1个睡眠周期的长度),以告诉您理想的起床时间。

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);

如何制作它,以便当我添加90到915时它会给我1045而不是1095?

4 个答案:

答案 0 :(得分:2)

以毫秒为单位计算所有内容。这可能起初看起来不方便,但数学很简单。

输入就寝时间时,您需要决定用户如何执行此操作。它会是几小时和几分钟。会有上午/下午或24小时制。你会考虑什么时区。所有这些都是通过Calendar或DateFormat类完成的,它将为您提供一个具有指定时间的长值。

之后,添加时间偏移很容易。

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)));

每个长值都包含警报应该关闭的时间。再次使用格式化程序生成该时间值的用户友好表示。

答案 1 :(得分:0)

首先,没有从4位数字到时间的直接转换。 因此,您需要使用Calendar

通过此,您可以获得当前日期/时间Calendar rightNow = Calendar.getInstance();

然后您可以使用add方法添加90分钟,请参阅http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#add(int,int)

答案 2 :(得分:0)

如果您将小时数分为60小时而不是100分钟,则将小时和分钟分开会更容易。要求用户输入他们的时间为“HH:MM”并使用split(“:”)解析它以分别获得小时和分钟:

    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]);

获取要添加的小时数和分钟数:

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

但是,我可能是Java的Calendar或Date类。他们将为您处理很多细节。

答案 3 :(得分:0)

可能不是您问题的确切答复。在这种情况下,您应该使用CalendarSimpleDateFormat,如下所示。

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);

对于输入0900,输出将是,

When will you be going to bed?
0900
You should set your alarm for: 1030 1200 1330 1500 1630 or 1800