I am using the below code to set an alarm. I would like to output what the time for this would be. I don't know if I going about this the wrong way. If I output the variable cal it has a long string of information. How do I extract only the hour and minutes?
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 464);
Date and Time in Android are formatted using the SimpleDateFormat library from Java, using Calendar instance which helps to get the current system date and time. The current date and time are of the type Long which can be converted to a human-readable date and time.
text. DateFormat. getDateTimeInstance(). format(new Date()); // textView is the TextView view that should display it textView.
You can use the static constants as m0skit0 says, or use SimpleDateFormat
. Here's some code to show both methods:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 464);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
System.out.println(sdf.format(cal.getTime()));
System.out.println(cal.get(Calendar.HOUR)+":"+cal.get(Calendar.MINUTE));
outputs:
05:31
5:31
Use the get() method on your Calendar
object, and use Calendar
static constants for the needed field (hour, minute, etc...).
For example:
cal.get(Calendar.Minute);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With