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?
Asked
Active
Viewed 7.9k times
10
-
1http://stackoverflow.com/questions/7953725/how-to-convert-milliseconds-to-date-format-in-android – Kirill Kulakov Aug 11 '12 at 09:10
-
3Didn't you search [here](http://stackoverflow.com/search?q=android+get+current+time) – Praveenkumar Aug 11 '12 at 09:16
-
1possible duplicate of [Android get current time and date](http://stackoverflow.com/questions/5369682/android-get-current-time-and-date) – Jürgen Thelen Aug 13 '12 at 04:29
-
just read the basic Java/Android docs on dates and time. including formatting the output. – toadzky Nov 13 '12 at 18:20
10 Answers
33
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
date.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
String localTime = date.format(currentLocalTime);
-
Sorry did forget to mention that this displays the time for GMT+1h. just change the `TimeZone.getTimeZone("GMT+1:00")` to your own need – Ahmad Aug 11 '12 at 09:52
-
I want to show if the time is 3:30 means i want to show it like 3:30pm but it shows 15:30.How it can be converted – Manikandan Aug 11 '12 at 10:03
-
Calendar c = Calendar.getInstance(); int mhour = c.get(Calendar.HOUR) – Jerin Raj Jan 05 '16 at 10:42
3
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");
date.setTimeZone(TimeZone.getTimeZone("GMT-4:00"));
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);

Raul Vazquez
- 39
- 3
3
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()

Mihir Patel
- 404
- 6
- 14
-
While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yennsarah Jul 14 '16 at 09:25
3
String currentDateAndTime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date());

Jaydev
- 1,794
- 20
- 37
0
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);

SkateJerrySkate
- 381
- 3
- 8
0
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.

Sarjan Desai
- 3,683
- 2
- 19
- 32

dondondon
- 881
- 8
- 4
0
You could use:
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
There are plenty of constants in Calendar for everything you need.

Eiad Olimat
- 13
- 6
0
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

pruthwiraj.kadam
- 1,033
- 10
- 11