Reputation: 11782
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?
Upvotes: 0
Views: 162
Reputation: 340300
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.
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.
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
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.
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?
Upvotes: 0
Reputation: 46239
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.
Upvotes: 4
Reputation: 6452
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.
Upvotes: 0