1. Are you ready for the Galaxy S20? Here is everything we know so far!

Carrying out time calculations?

Discussion in 'Android Development' started by RED_, Jan 12, 2013.

  1. RED_

    RED_ Well-Known Member
    Thread Starter

    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.
     


  2. myrecek

    myrecek Newbie

    The right usage for Calendar.add method is something like :

    [HIGH]add(Calendar.HOUR_OF_DAY, 6)[/HIGH]

    But it hasn't much in common with Android...
     
  3. RED_

    RED_ Well-Known Member
    Thread Starter

    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.
     
  4. RED_

    RED_ Well-Known Member
    Thread Starter

    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.
     
  5. jonbonazza

    jonbonazza Android Expert

    Well, date1 is null when you pass it into the DateTime() constructor. Thats gonna cause problems.

    Also, youll need to format the date, i assume. Not sure how you do it with the library you are using though.
     
  6. RED_

    RED_ Well-Known Member
    Thread Starter

    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
     
  7. jungajuice

    jungajuice Newbie

    Why not just construct it as
    [HIGH]Text1.setText("Time: " + localTime.getHourOfDay() + ":" + localtime.getMinuteOfHour());[/HIGH]
     
  8. RED_

    RED_ Well-Known Member
    Thread Starter

    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.
     
  9. jonbonazza

    jonbonazza Android Expert

    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.
     
  10. RED_

    RED_ Well-Known Member
    Thread Starter

    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.
     
Loading...

Share This Page

Loading...