如何进行倒计时&;blackberry storm的java计数应用程序
您好,我想创建一个黑莓应用程序,用于倒计时和倒计时,例如,如果您输入您的出生日期,它应该会生成多少年、月、日、小时和分钟过去了,我想知道如何创建正确的逻辑来创建此应用程序。提前感谢……使用基本的Java SE 6 API,您最好的选择是。它也可以在中找到。因为没有内置的工具来计算周期,所以只需要大量的代码来正确计算经过的时间。您需要克隆日历实例,并在计数循环中添加年、月和日,直到它到达结束日期。你不能只除以秒,因为这不包括闰年,白天的储蓄和诸如此类的东西。计算完后,你可以按照通常的方式将剩余的时间分为小时、分钟和秒 以下是一个启动示例:如何进行倒计时&;blackberry storm的java计数应用程序,java,Java,您好,我想创建一个黑莓应用程序,用于倒计时和倒计时,例如,如果您输入您的出生日期,它应该会生成多少年、月、日、小时和分钟过去了,我想知道如何创建正确的逻辑来创建此应用程序。提前感谢……使用基本的Java SE 6 API,您最好的选择是。它也可以在中找到。因为没有内置的工具来计算周期,所以只需要大量的代码来正确计算经过的时间。您需要克隆日历实例,并在计数循环中添加年、月和日,直到它到达结束日期。你不能只除以秒,因为这不包括闰年,白天的储蓄和诸如此类的东西。计算完后,你可以按照通常的方式将剩余的时
package com.stackoverflow.q2936686;
import java.util.Calendar;
public class ElapsedTimeWithCalendar {
public static void main(String[] args) throws Exception {
Calendar birthDate = Calendar.getInstance();
birthDate.set(1978, 3 - 1, 26, 12, 35, 0); // My birthdate.
Calendar now = Calendar.getInstance(); // Now.
Integer[] elapsed = new Integer[6];
Calendar clone = (Calendar) birthDate.clone(); // Otherwise changes are been reflected.
elapsed[0] = elapsed(clone, now, Calendar.YEAR);
clone.add(Calendar.YEAR, elapsed[0]);
elapsed[1] = elapsed(clone, now, Calendar.MONTH);
clone.add(Calendar.MONTH, elapsed[1]);
elapsed[2] = elapsed(clone, now, Calendar.DATE);
clone.add(Calendar.DATE, elapsed[2]);
elapsed[3] = (int) (now.getTimeInMillis() - clone.getTimeInMillis()) / 3600000;
clone.add(Calendar.HOUR, elapsed[3]);
elapsed[4] = (int) (now.getTimeInMillis() - clone.getTimeInMillis()) / 60000;
clone.add(Calendar.MINUTE, elapsed[4]);
elapsed[5] = (int) (now.getTimeInMillis() - clone.getTimeInMillis()) / 1000;
System.out.format("%d years, %d months, %d days, %d hours, %d minutes, %d seconds%n", elapsed);
}
private static int elapsed(Calendar before, Calendar after, int field) {
Calendar clone = (Calendar) before.clone(); // Otherwise changes are been reflected.
int elapsed = -1;
while (!clone.after(after)) {
clone.add(field, 1);
elapsed++;
}
return elapsed;
}
}
非常详细,是的。然而,在JavaSE7中,一个新的日期和时间API即将出现(),它将与当前可用的API类似。下面是一个使用JodaTime的启动示例:
DateTime birthDate = new DateTime(1978, 3, 26, 12, 35, 0, 0);
DateTime now = new DateTime();
Period elapsed = new Period(birthDate, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" years, ")
.appendMonths().appendSuffix(" months, ")
.appendDays().appendSuffix(" days, ")
.appendHours().appendSuffix(" hours, ")
.appendMinutes().appendSuffix(" minutes, ")
.appendSeconds().appendSuffix(" seconds")
.toFormatter();
System.out.println(formatter.print(elapsed));
有什么不同,嗯?:) 使用基本的JavaSE6API,您最好的选择是。它也可以在中找到。因为没有内置的工具来计算周期,所以只需要大量的代码来正确计算经过的时间。您需要克隆日历实例,并在计数循环中添加年、月和日,直到它到达结束日期。你不能只除以秒,因为这不包括闰年,白天的储蓄和诸如此类的东西。计算完后,你可以按照通常的方式将剩余的时间分为小时、分钟和秒 以下是一个启动示例:
package com.stackoverflow.q2936686;
import java.util.Calendar;
public class ElapsedTimeWithCalendar {
public static void main(String[] args) throws Exception {
Calendar birthDate = Calendar.getInstance();
birthDate.set(1978, 3 - 1, 26, 12, 35, 0); // My birthdate.
Calendar now = Calendar.getInstance(); // Now.
Integer[] elapsed = new Integer[6];
Calendar clone = (Calendar) birthDate.clone(); // Otherwise changes are been reflected.
elapsed[0] = elapsed(clone, now, Calendar.YEAR);
clone.add(Calendar.YEAR, elapsed[0]);
elapsed[1] = elapsed(clone, now, Calendar.MONTH);
clone.add(Calendar.MONTH, elapsed[1]);
elapsed[2] = elapsed(clone, now, Calendar.DATE);
clone.add(Calendar.DATE, elapsed[2]);
elapsed[3] = (int) (now.getTimeInMillis() - clone.getTimeInMillis()) / 3600000;
clone.add(Calendar.HOUR, elapsed[3]);
elapsed[4] = (int) (now.getTimeInMillis() - clone.getTimeInMillis()) / 60000;
clone.add(Calendar.MINUTE, elapsed[4]);
elapsed[5] = (int) (now.getTimeInMillis() - clone.getTimeInMillis()) / 1000;
System.out.format("%d years, %d months, %d days, %d hours, %d minutes, %d seconds%n", elapsed);
}
private static int elapsed(Calendar before, Calendar after, int field) {
Calendar clone = (Calendar) before.clone(); // Otherwise changes are been reflected.
int elapsed = -1;
while (!clone.after(after)) {
clone.add(field, 1);
elapsed++;
}
return elapsed;
}
}
非常详细,是的。然而,在JavaSE7中,一个新的日期和时间API即将出现(),它将与当前可用的API类似。下面是一个使用JodaTime的启动示例:
DateTime birthDate = new DateTime(1978, 3, 26, 12, 35, 0, 0);
DateTime now = new DateTime();
Period elapsed = new Period(birthDate, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" years, ")
.appendMonths().appendSuffix(" months, ")
.appendDays().appendSuffix(" days, ")
.appendHours().appendSuffix(" hours, ")
.appendMinutes().appendSuffix(" minutes, ")
.appendSeconds().appendSuffix(" seconds")
.toFormatter();
System.out.println(formatter.print(elapsed));
有什么不同,嗯?:)