如何在android中获取当前时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11913358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:09:59  来源:igfitidea点击:

How to get the current time in android

android

提问by Manikandan

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?

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

回答by Ahmad

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

回答by Furqi

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

// textView is the TextView view that should display it
textView.setText(currentDateTimeString);

回答by Hiren

I get perfect time using below..

我得到完美的时间使用下面..

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

字符串时间 = 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());

字符串 date_L = java.text.DateFormat。getDateInstance().format(new Date());

回答by Raul Vazquez

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.

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

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

回答by Mihir Patel

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

回答by Jaydev

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

回答by SkateJerrySkate

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

回答by januprasad

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

你可以通过 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

为了存储在数据库中,请在此处阅读 sqlite 教程

回答by dondondon

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.

或查看本教程

回答by Eiad Olimat

You could use:

你可以使用:

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

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

日历中有大量常量可满足您的所有需求。