Reputation: 1832
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?
Upvotes: 10
Views: 80342
Reputation: 1591
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());
Upvotes: 4
Reputation: 1093
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
Upvotes: 0
Reputation: 1812
String currentDateAndTime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date());
Upvotes: 3
Reputation: 412
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()
Upvotes: 3
Reputation: 13
You could use:
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
There are plenty of constants in Calendar for everything you need.
Upvotes: 0
Reputation: 72553
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);
Upvotes: 33
Reputation: 901
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.
Upvotes: 0
Reputation: 39
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);
Upvotes: 3
Reputation: 381
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);
Upvotes: 0