Home » Android » How to get the current time in android

How to get the current time in android

Posted by: admin May 14, 2020 Leave a comment


I want to get the current time in Android. It should be displayed like 2:30pm and I want to store it in a database. I searched a lot. But I don’t know how to do it. How can I achieve this?

How to&Answers:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("HH:mm a"); 
// you can get seconds by adding  "...:ss" to it

String localTime = date.format(currentLocalTime); 


String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

// textView is the TextView view that should display it


I get perfect time using below..

String Time = java.text.DateFormat.getTimeInstance().format(new Date());

and for to get date try it as below

String date_L = java.text.DateFormat.getDateInstance().format(new Date());


To get the time in a 12 hour format try this code with “KK” instead of “HH”. A full explanation of the symbols you can use are here.

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-4:00"));
     Date currentLocalTime = cal.getTime();
     DateFormat date = new SimpleDateFormat("KK:mm");
     String localTime = date.format(currentLocalTime); 

Also to get the hour, minutes and seconds as integers you can use:

int currentHour = cal.get(Calendar.HOUR);
int currentMinutes = cal.get(Calendar.MINUTE);
int currentSeconds = cal.get(Calendar.SECOND);


Following method gives you to current date and time in specified date format

         * getCurrentTime() it will return system time
         * @return
        public static String getCurrentTime() {     
           //date output format
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            return dateFormat.format(cal.getTime());
        }// end of getCurrentTime()


Easy, you can dissect the time to get separate values for current time, as follows:

Calendar cal = Calendar.getInstance(); 

  int millisecond = cal.get(Calendar.MILLISECOND);
  int second = cal.get(Calendar.SECOND);
  int minute = cal.get(Calendar.MINUTE);
        //12 hour format
  int hour = cal.get(Calendar.HOUR);
        //24 hour format
  int hourofday = cal.get(Calendar.HOUR_OF_DAY);


String currentDateAndTime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date());


You can get time via
Date dt = new Date();

        int hours = dt.getHours();
        int minutes = dt.getMinutes();
        int seconds = dt.getSeconds();

and for storing in db,read sqlite tutorial here


use digital clock widget when you want a current time:

Calendar mCalendar;
private final static String m12 = "h:mm aa";
private final static String m24 = "k:mm";
private FormatChangeObserver mFormatChangeObserver;

private Runnable mTicker;
private Handler mHandler;

private boolean mTickerStopped = false;

String mFormat;

or check this tutorial.


You could use:

Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);

There are plenty of constants in Calendar for everything you need.


SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
Date date = Calendar.getInstance().getTime();
String sDate = format.format(date);//31-12-9999
int mYear = c.get(Calendar.YEAR);//9999
int mMonth = c.get(Calendar.MONTH);
mMonth = mMonth + 1;//12
int hrs = c.get(Calendar.HOUR_OF_DAY);//24
int min = c.get(Calendar.MINUTE);//59