2010-02-16 131 views

回答

255

好吧,并不难,因为有几种方法可以做到这一点。我假设你想把当前日期&时间放入TextView

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

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

还有更多可以很容易地找到here 文档中读取。您可以在这里找到更多关于如何更改用于转换的格式的信息。

+2

得到错误在getDateInstance()和新的Date() – BIBEKRBARAL 2010-02-16 07:53:50

+39

请 - 更加明确!什么是错误?你输入了错误的DateFormat类吗?它是'java.text.DateFormat'而不是'android.text.format.DateFormat'!它是'java.util.Date'而不是'java.sql.Date'! 只是提出一些问题提示:尽量准确,例如:在你的问题中声明你的意思是“显示”。当你输入我的行时 - 当然,Date和DateFormat必须被导入 - 如果每个选项都是2,你可以尝试的最少是任意组合:它只有4! – Zordid 2010-02-16 11:08:09

+0

非常感谢你sir.i得到了时间 – BIBEKRBARAL 2010-02-17 04:54:47

34

显示时间的明显选择是AnalogClock ViewDigitalClock View

例如,以下布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <AnalogClock 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

    <DigitalClock 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:textSize="20sp"/> 
</LinearLayout> 

是这样的:

screenshot

+3

亲爱的先生,我想使用setText显示当前时间。 – BIBEKRBARAL 2010-02-16 09:19:51

+5

看完这个明显的答案后,我觉得自己像个笨蛋!我实现了自己的runnable,让它睡了一段时间,等等,当明显的答案是XML-one-liner!非常感谢(在您发布后超过一年):-) – dbm 2011-02-22 08:08:29

+5

2015年已弃用,建议您使用TextClock代替。 :) – Evilripper 2015-02-27 09:45:27

49
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 
    Thread myThread = null; 

    Runnable runnable = new CountDownRunner(); 
    myThread= new Thread(runnable); 
    myThread.start(); 

} 

public void doWork() { 
    runOnUiThread(new Runnable() { 
     public void run() { 
      try{ 
       TextView txtCurrentTime= (TextView)findViewById(R.id.lbltime); 
        Date dt = new Date(); 
        int hours = dt.getHours(); 
        int minutes = dt.getMinutes(); 
        int seconds = dt.getSeconds(); 
        String curTime = hours + ":" + minutes + ":" + seconds; 
        txtCurrentTime.setText(curTime); 
      }catch (Exception e) {} 
     } 
    }); 
} 


class CountDownRunner implements Runnable{ 
    // @Override 
    public void run() { 
      while(!Thread.currentThread().isInterrupted()){ 
       try { 
       doWork(); 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
         Thread.currentThread().interrupt(); 
       }catch(Exception e){ 
       } 
      } 
    } 
} 
+0

请提供runOnUiThread函数的定义。 – 2011-10-24 11:30:13

+0

@Harshit只要你的类扩展了这个函数就可以使用Android SDK活动 – 2012-01-08 16:24:16

+2

我知道这是个老问题,但是如果有人会像我一样在谷歌中找到它,他应该知道Date.getX方法已被弃用。 – tobi 2012-07-20 09:28:05

21

我自己工作的解决方案:

Calendar c = Calendar.getInstance(); 

String sDate = c.get(Calendar.YEAR) + "-" 
+ c.get(Calendar.MONTH) 
+ "-" + c.get(Calendar.DAY_OF_MONTH) 
+ " at " + c.get(Calendar.HOUR_OF_DAY) 
+ ":" + c.get(Calendar.MINUTE); 

希望日是有帮助的!

+0

我想知道为什么c.get(Calendar.MONTH)返回5时,它应该是6?我的设备具有正确的时间设置。 – Kris 2011-06-09 10:45:32

+3

0索引也许? – 2011-06-12 19:58:48

+0

哦,是的,但为什么他们必须这样做时,其他变量是准确的。 :) – Kris 2011-06-13 06:11:30

4

这将使当前日期和时间:

public String getCurrDate() 
{ 
    String dt; 
    Date cal = Calendar.getInstance().getTime(); 
    dt = cal.toLocaleString(); 
    return dt; 
} 
12

How to get full date with correct format?

请使用

android.text.format.DateFormat.getDateFormat(Context context) 
android.text.format.DateFormat.getTimeFormat(Context context) 

得到有效的时间和日期格式的当前用户设置的意义(例如12/24时间格式)。

import android.text.format.DateFormat; 

private void some() { 
    final Calendar t = Calendar.getInstance(); 
    textView.setText(DateFormat.getTimeFormat(this/*Context*/).format(t.getTime())); 
} 
109
public class XYZ extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.main); 

     Calendar c = Calendar.getInstance(); 
     System.out.println("Current time => "+c.getTime()); 

     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     String formattedDate = df.format(c.getTime()); 
     // formattedDate have current date/time 
     Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show(); 


     // Now we display formattedDate value in TextView 
     TextView txtView = new TextView(this); 
     txtView.setText("Current Date and Time : "+formattedDate); 
     txtView.setGravity(Gravity.CENTER); 
     txtView.setTextSize(20); 
     setContentView(txtView); 
    } 

} 

enter image description here

+0

android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_CODES.N(24)。 – kangear 2017-02-10 07:57:49

+0

如何声明'SimpleDateFormat',因为我找不到符号类 – dubis 2017-08-11 08:36:51

16

如果你想在一个特定的模式的日期和时间,您可以使用

Date d = new Date(); 
CharSequence s = DateFormat.format("yyyy-MM-dd hh:mm:ss", d.getTime()); 
4
Calendar c = Calendar.getInstance(); 
int month=c.get(Calendar.MONTH)+1; 
String sDate = c.get(Calendar.YEAR) + "-" + month+ "-" + c.get(Calendar.DAY_OF_MONTH) + 
"T" + c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND); 

这会给日期时间格式像 2010 -05-24T18:13:00

2

试试下面的代码:

SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy/MM/dd HH:mm:ss"); 

Calendar cal = Calendar.getInstance(); 
System.out.println("time => " + dateFormat.format(cal.getTime())); 

String time_str = dateFormat.format(cal.getTime()); 

String[] s = time_str.split(" "); 

for (int i = 0; i < s.length; i++) { 
    System.out.println("date => " + s[i]); 
} 

int year_sys = Integer.parseInt(s[0].split("/")[0]); 
int month_sys = Integer.parseInt(s[0].split("/")[1]); 
int day_sys = Integer.parseInt(s[0].split("/")[2]); 

int hour_sys = Integer.parseInt(s[1].split(":")[0]); 
int min_sys = Integer.parseInt(s[1].split(":")[1]); 

System.out.println("year_sys => " + year_sys); 
System.out.println("month_sys => " + month_sys); 
System.out.println("day_sys => " + day_sys); 

System.out.println("hour_sys => " + hour_sys); 
System.out.println("min_sys => " + min_sys); 
6
String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()); 

使用formattedDate作为填充日期之前,您String
对我而言:mDateButton.setText(formattedDate);

7

这是我工作的代码。请试试这个。这是一个简单的方法,它需要系统调用的时间和日期。 Datetime()方法,无论你需要什么。

public static String Datetime() 
{ 
    Calendar c = Calendar .getInstance(); 
    System.out.println("Current time => "+c.getTime()); 
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mms"); 
    formattedDate = df.format(c.getTime()); 
    return formattedDate; 
} 
8

用途:

Calendar c = Calendar.getInstance(); 

int seconds = c.get(Calendar.SECOND); 
int minutes = c.get(Calendar.MINUTE); 
int hour = c.get(Calendar.HOUR); 
String time = hour + ":" + minutes + ":" + seconds; 


int day = c.get(Calendar.DAY_OF_MONTH); 
int month = c.get(Calendar.MONTH); 
int year = c.get(Calendar.YEAR); 
String date = day + "/" + month + "/" + year; 

// Assuming that you need date and time in a separate 
// textview named txt_date and txt_time. 

txt_date.setText(date); 
txt_time.setText(time); 
4

只需复制此代码,并希望这个作品对你罚款。

Calendar c = Calendar.getInstance(); 
SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a"); 
String strDate = sdf.format(c.getTime()); 
19

如果你想要的一行代码:

String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()); 

结果是"2016-09-25 16:50:34"

+0

(y)很好的回答:) – 2017-03-24 20:19:19

3

显示当前日期函数:

Calendar c = Calendar.getInstance(); 

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); 
String date = df.format(c.getTime()); 
Date.setText(date); 

你必须要进口

import java.text.SimpleDateFormat; import java.util.Calendar;

你必须要使用

TextView Date; 
Date = (TextView) findViewById(R.id.Date); 
2
String currentDateandTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 
Toast.makeText(getApplicationContext(), currentDateandTime, Toast.LENGTH_SHORT).show(); 
0

为了得到当前时间/日期只需使用下面的代码片段:

要使用时间

SimpleDateFormat simpleDateFormatTime = new SimpleDateFormat("HH:mm", Locale.getDefault()); 
String strTime = simpleDateFormatTime.format(now.getTime()); 

要使用日期

SimpleDateFormat simpleDateFormatDate = new SimpleDateFormat("E, MMM dd, yyyy", Locale.getDefault());  
String strDate = simpleDateFormatDate.format(now.getTime()); 

,你是好去。

0

对于显示当前日期和时间上的TextView

/// For Show Date 
    String currentDateString = DateFormat.getDateInstance().format(new Date()); 
    // textView is the TextView view that should display it 
    textViewdate.setText(currentDateString); 
    /// For Show Time 
    String currentTimeString = DateFormat.getTimeInstance().format(new Date()); 
    // textView is the TextView view that should display it 
    textViewtime.setText(currentTimeString); 

检查全码Android – Display the current date and time in an Android Studio Example with source code

相关问题