Get Current Date/Time

Home

The correct way to retrieve the current time is to use the Calendar object. The syntax is not immediately obvious, so here is an example:

// Calculate and set current server time (hh:mm):
Calendar current = Calendar.getInstance();
int hours = current.get(Calendar.HOUR_OF_DAY);
int minutes = current.get(Calendar.MINUTE);
String sServerTime = Integer.toString(hours) + ":" + Integer.toString(minutes);