How do you set the time and only the time in a calendar in Java?
From here: calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute);
From here: calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute);
^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$ Explanation: ^ # Start of string (?: # Try to match… (?: # Try to match… ([01]?\d|2[0-3]): # HH: )? # (optionally). ([0-5]?\d): # MM: (required) )? # (entire group optional, so either HH:MM:, MM: or nothing) ([0-5]?\d) # SS (required) $ # End of string
Try this: Calendar calendar = Calendar.getInstance(); calendar.setTime(yourdate); int hours = calendar.get(Calendar.HOUR_OF_DAY); int minutes = calendar.get(Calendar.MINUTE); int seconds = calendar.get(Calendar.SECOND); Edit: hours, minutes, seconds above will be the hours, minutes and seconds after converting yourdate to System Timezone!
Brief Description The answer from Brian Ramsay is more convenient if you only want to convert to minutes. If you want Cocoa API do it for you and convert your NSTimeInterval not only to minutes but also to days, months, week, etc,… I think this is a more generic approach Use NSCalendar method: (NSDateComponents *)components:(NSUInteger)unitFlags … Read more
The answers above are for older versions of PHP. Use the DateTime class to do any date calculations now that PHP 5.3 is the norm. Eg. $start_date = new DateTime(‘2007-09-01 04:10:58’); $since_start = $start_date->diff(new DateTime(‘2012-09-11 10:25:00′)); echo $since_start->days.’ days total<br>’; echo $since_start->y.’ years<br>’; echo $since_start->m.’ months<br>’; echo $since_start->d.’ days<br>’; echo $since_start->h.’ hours<br>’; echo $since_start->i.’ minutes<br>’; … Read more