10

我想在Android中获取当前时间。它应该显示为下午 2:30,我想将其存储在数据库中。我搜索了很多。但我不知道该怎么做。我怎样才能做到这一点?

4

11 回答 11

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); 
于 2012-08-11T09:09:59.777 回答
9
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

// textView is the TextView view that should display it
textView.setText(currentDateTimeString);
于 2012-08-11T09:13:57.783 回答
4

我在下面得到了完美的时间......

 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());
于 2017-03-30T09:11:18.750 回答
3

要获得 12 小时格式的时间,请尝试使用“KK”而不是“HH”的代码。您可以使用的符号的完整说明在这里

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); 

还可以使用以下整数获取小时、分钟和秒:

int currentHour = cal.get(Calendar.HOUR);
int currentMinutes = cal.get(Calendar.MINUTE);
int currentSeconds = cal.get(Calendar.SECOND);
于 2015-06-12T02:03:30.173 回答
3

以下方法以指定日期格式为您提供当前日期和时间

 /**
         * 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()
于 2016-07-13T12:10:44.880 回答
3
String currentDateAndTime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date());
于 2017-01-30T09:55:59.177 回答
0

你可以通过 Date dt = new Date();

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

并且为了存储在数据库中,请阅读 sqlite 教程here

于 2012-08-11T09:09:16.920 回答
0

很简单,您可以剖析时间以获取当前时间的单独值,如下所示:

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);
于 2012-11-13T18:18:17.227 回答
0

当您需要当前时间时使用数字时钟小部件:

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;

或查看本教程

于 2015-10-15T09:17:15.593 回答
0

你可以使用:

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

日历中有很多常量可以满足您的一切需求。

于 2016-05-31T18:18:29.323 回答
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
于 2018-03-12T06:33:18.883 回答