10

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?

Ahmad
  • 69,608
  • 17
  • 111
  • 137
Manikandan
  • 1,802
  • 5
  • 20
  • 28

10 Answers10

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); 
CaptJak
  • 3,592
  • 1
  • 29
  • 50
Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • 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
4

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());
saed
  • 43
  • 1
  • 6
Hiren
  • 1,581
  • 1
  • 12
  • 14
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);
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

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

J.K
  • 2,290
  • 1
  • 18
  • 29
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.

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