• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Carrying out time calculations?

RED_

Well-Known Member
Nov 13, 2010
209
8
London
Can anyone help guide me in the right direction? I want to be able to let a button do some time calculations for me.

Basically I want the user to able to press the button and it will take the system time on their device and add numbers to it.

This is the code I have so far:

[HIGH]public void onClickHere (View v) {

Calendar time = Calendar.getInstance();

int hourofday = time.get(Calendar.HOUR_OF_DAY);
int minutes = time.get(Calendar.MINUTE);
time.add(hourofday, 6);
time.add(minutes, 14);

}[/HIGH]

The code above is just an example and it takes the system time and puts it in a TextView. It will also add 6 to the hours and 14 to the minutes. The problem is I get force close errors when the time is something like 1:50pm. You can't add 14minutes to 50minutes. Is there another direction I can take so that it adds up correctly and I can add any number of hours and minutes to any time?

Thanks.
 
So if I did that with minutes would it add time correctly? Allowing it to roll over into the next hour with no force closes?

I know this is the pure java way of doing it but it's the only way I've learnt so far. Are you suggesting there's a better way to add time in Android?

Thanks.
 
Upvote 0
I've added the Joda-Time API since so many sites are recommending it.

If anyone can help carry out time calculations using Joda-Time I would appreciate it.

I have the following but it doesn't work:

[HIGH]public void onClickHere (View v) {

Date date1 = null;
DateTime dt;

dt = new DateTime(date1);
dt.plusMinutes(10);

Text1.setText("Time:"+dt);

}[/HIGH]

Currently it only displays the date & time in full with milliseconds and whatever else.
 
Upvote 0
Been working on it all day and I've come up with this:

[HIGH]public void onClickHere (View v) {

LocalTime localtime = new LocalTime();

LocalTime dt = new LocalTime(localtime.getHourOfDay(), localtime.getMinuteOfHour());
LocalTime twoHoursLater = dt.plusHours(2);

Text1.setText("Time: " + twoHoursLater);

}[/HIGH]

This displays the time with 2 hours added on so it works. The only issue I have now is only getting it to show the hour and minutes. It's showing the hour, minute, second and millisecond information at the moment. Getting closer and closer..

Because I called getHourOfDay() & getMinuteOfHour() it's only showing numbers for those two and 0's for the rest. So it displays 18:16:00.000
 
Upvote 0
I solved it by doing the following:

[HIGH]public void getWakeUpTime (View v) {

LocalTime localtime = new LocalTime();
LocalTime dt = new LocalTime(localtime.getHourOfDay(), localtime.getMinuteOfHour());
LocalTime twoHoursLater = dt.plusHours(2);

DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
Text1.setText("Time: " + twoHoursLater.toString(formatter));[/HIGH]

The formatter lets me decide which way I want it displayed.

Now I need to find out how to show this information on a new layout. So when I click the button it displays the time on a new page rather than on the same page. Spent the morning trying to do it with layout inflators but getting nowhere. Might start a new thread if I don't solve it by the end of the day.
 
Upvote 0
If its supposed to be an entirely new "page", then you should use a seperate activity. Then, you can just pass the string in with the Intent object and grab it when the new activity is created.

Yup. I did exactly this about an hour ago. TextView in new activity, String in current activity and it sends it over via an Intent.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones