开发者

how make a count down & count up application in java for blackberry storm

开发者 https://www.devze.com 2023-01-01 18:29 出处:网络
Hi there i want to create a blackberry application for countdown and count up dates like for example if you enter your birth date it should generate how many years, months, days, hours and minutes hav

Hi there i want to create a blackberry application for countdown and count up dates like for example if you enter your birth date it should generate how many years, months, days, hours and minutes have passed and i want to know how to create the开发者_如何学编程 proper logic to create this application. thanks in advance....


With the basic Java SE 6 API, your best bet is the java.util.Calendar. It's also available in Java ME. It's only going to be a lot of code to calculate the elapsed time properly since there are no builtin facilities to calculate the period. You need to clone the calendar instance and add the years, months and days inside a counting loop until it has reached the end date. You cannot just divide by seconds, because that wouldn't take leap years, daytime savings and that kind of stuff into account. After calculating that, you can divide the remnant into hours, minutes and seconds the usual way.

Here's a kickoff example:

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

}

Pretty verbose, yes. However, in Java SE 7 a new Date and Time API is coming (JSR-310) which is going to be similar as the currently available JodaTime API. Here's a kickoff example how it would look like using 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));

What a difference, huh? :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号