Get Current Time By Using java.util.Calendar#getInstance()

Below is a simple example for showing how to use Calendar.getInstance() to get current time.

public static String getCurrentTime(){
  Calendar c=Calendar.getInstance();
  String hour="" + c.get(Calendar.HOUR_OF_DAY);
  String min="" + c.get(Calendar.MINUTE);
  String sec="" + c.get(Calendar.SECOND);
  if (hour.length() == 1)   hour="0" + hour;
  if (min.length() == 1)   min="0" + min;
  if (sec.length() == 1)   sec="0" + sec;
  return hour + ":" + min+ ":"+ sec;
}

Reference:

1. java.util.Caldendar

Leave a Comment