0

I have a bunch of data containing a time value, e.g. 09:30am, and a duration, either 15,30,45 or 60. What would be the best way of getting the end time from these values? Will it be some use of a Date / Calendar or would just a custom function be quicker to do?

TMH
  • 6,096
  • 7
  • 51
  • 88

3 Answers3

1

The Joda Time library (http://www.joda.org/joda-time/) is popular, easy to work with, and handles these kinds of calculations in a straight-forward manner.

DateTime start = new DateTime(2013, 10, 15, 9, 30); // create a DateTime representing Oct 15, 2013 at 9:30
Duration dur = Duration.standardMinutes(15); // create a duration of 15 minutes
DateTime calc = start.plus(dur); // add; result is 9:45 on 10/15/2013
weston
  • 54,145
  • 21
  • 145
  • 203
Matthew Burke
  • 2,295
  • 17
  • 14
1

The standard java/android way for this is to use a calendar:

    final Calendar c = Calendar.getInstance(); // now

    c.add(Calendar.DAY_OF_MONTH, 10); // add 10 days

There are other constants if you want to add minutues, years, seconds, ....

k3b
  • 14,517
  • 7
  • 53
  • 85
0

Check out this solution: Java string to date conversion

You may need to change the "MMMM d, yyyy" portion to fit your data set.

Here is the Javadoc: http://developer.android.com/reference/java/text/SimpleDateFormat.html

edit: For the second part of your question, there are several questions on here about how to add to a Date/Calendar object.

Community
  • 1
  • 1
telkins
  • 10,440
  • 8
  • 52
  • 79