0

I am trying to use

import android.text.format.Time;
Time startTime = new Time();
startTime.hour = 6;
startTime.minute = 0;

Now after calculations , when I do startTime.minute = startTime.minute + xx and it exceeds 60 min, it wont get to zero and won't change the hour of startTime.

How can I do that?

Zamani
  • 306
  • 3
  • 15
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

3 Answers3

4

You need to check for overflow yourself:

if (startTime.minute + xx >= 60) {
  startTime.hour += 1;
}
startTime.minute = (startTime.minute + xx) % 60;

[EDIT]

As @Jmelnik:s answer and @VikramBodicherla:scomment states, you should definitely have a look at the Calendar class or something similarly advanced if you are doing something other than the very basic operation you show in your code snippet.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • This is not advisable. Programmers have long suffered converting minutes to seconds or hours because time is a complex thing. Timezones, leap years, leap seconds, DST! Which is the why the more elaborate (and complex) Calendar class has been created. Use the Calendar class. – Vikram Bodicherla Jul 20 '12 at 06:47
  • @VikramBodicherla: I almost agree. I think it is fine here, but as soon as it gets any more complicated `Calendar` is definitely much better suited. Edited to mention that. – Keppil Jul 20 '12 at 06:53
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 06 '18 at 00:40
0

Basically I suggest you using standard Java APIs, which up to Android APIs were able to such things, which makes it better documented and search engines are full of examples.

Simply your code would look like this:

//1. Instantiate Date
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);

//2. Manipulate it with Calendar
cal.set(Calendar.HOUR_OF_DAY, 6);
cal.set(Calendar.MINUTE, 0);
cal.add(Calendar.MINUTE, 65);

//3. Format it with DateFormat
DateFormat df = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("GMT"));

String time = df.format(cal.getTime()); //07:05

Don't forget, that SimpleDateFormat will default to your system TimeZone and Locale if you don't specifically set it.

Run this code online.

d1e
  • 6,372
  • 2
  • 28
  • 41
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 06 '18 at 00:40
0

tl;dr

ZonedDateTime.now()        // Current moment captured in the JVM’s current default time zone. Better to explicitly pass the desired/expected time zone rather than rely implicitly on the default.
.with(                     // Adjust value of the original object to produce a new object. Using immutable objects pattern.
    LocalTime.of( 6 , 0 )  // 6 AM specified in a time-of-day-only object.
)                          // Returns a new `ZonedDateTime` object. If the specified time-of-day were not valid on that date in that zone, the time would be adjusted as needed.

java.time

The modern approach uses the java.time classes that supplanted the troublesome old date-time classes such as Date & Calendar.

If you meant android.text.format.Time, that class was deprecated in Android 22. Now supplanted by the ZonedDateTime class.

Your Question is unclear about whether you wanted time-of-day only or date-with-time-of-day values.

Time-of-day only

For a time-of-day value without date and without time zone, use LocalTime.

LocalTime lt = LocalTime.of( 6 , 0 ) ;  // 6 AM.
LocalTime minutesLater = lt.plusMinutes( 70 ) ;  // Add any number of minutes.

minutesLater.toString(): 07:10

Note that LocalTime is limited to single 24-hour stretch of time. The adding seven hours to 11 PM gets you 6 AM.

LocalTime lt = LocalTime.of( 23 , 0 );
LocalTime later = lt.plusHours( 7 );

later.toString(): 06:00

Date with time

If you were interested in a date-with-time-of-day, then use ZonedDateTime with LocalTime class. And of course you will need the ZoneId class as well, to get the current date and time. For any given moment, the date and time-of-day both vary around the globe by zone.

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdtNow = ZonedDateTime.now( z ) ;  // Capture the current moment according to the wall-clock time used by the people of a certain region (a time zone).

Specify the time-of-day, and adjust. If your specified time-of-day happens to not be valid in that zone on that date such as during a Daylight Saving Time (DST) cut-over, the ZonedDateTime class adjusts as necessary. Be sure to read the documentation to understand its algorithms for that adjustment.

LocalTime lt = LocalTime.of( 6 , 0 ) ;  // 6 AM.
ZonedDateTime zdtSixAmToday = zdtNow.with( lt ) ;  // Same date, same time zone, different time-of-day. Will be adjusted if your given time-of-day is not valid on that date in that zone.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154