2012-10-17 13 views
10

Ich weiß nicht, was unten mit Code falsch ist -Android aktuelles Datum erhalten und zeigt es in Textview

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    TextView tv = (TextView) findViewById(R.id.textView1); 

    String dt; 
    Date cal = (Date) Calendar.getInstance().getTime(); 
    dt = cal.toLocaleString(); 
    tv.setText(dt.toString()); 

} 
+0

Was tun Sie falsch sein vermuten angezeigt werden oder welche Fehler Sie begegnen Sie? – devrys

Antwort

25

Ich schlage vor:

long date = System.currentTimeMillis(); 

SimpleDateFormat sdf = new SimpleDateFormat("MMM MM dd, yyyy h:mm a"); 
String dateString = sdf.format(date); 
tvDisplayDate.setText(dateString); 

die

im folgende Beispiel Format zeigt

Mon Jan 5, 2009 4:55 PM

Sie können das gewünschte Format verwenden - Optionen können gefunden werden at Oracle

2
TextView tv = (TextView) findViewById(R.id.textView1); 
    SimpleDateFormat dfDate_day= new SimpleDateFormat("E, dd/MM/yyyy HH:mm:ss"); 
    String dt=""; 
    Calendar c = Calendar.getInstance(); 
    data=dfDate_day.format(c.getTime()); 
    tv.setText(dt); 
4

Below Lösung könnte helfen -

final Calendar c = Calendar.getInstance(); 
mYear = c.get(Calendar.YEAR); 
mMonth = c.get(Calendar.MONTH); 
mDay = c.get(Calendar.DAY_OF_MONTH); 

updateDisplay(); 

private void updateDisplay() { 
    mDateDisplay.setText(
     new StringBuilder() 
     // Month is 0 based so add 1 
     .append(mMonth + 1).append("-") 
     .append(mDay).append("-") 
     .append(mYear).append(" ")); 
} 
15

Verwendung dieser Code:

tvDisplayDate = (TextView) findViewById(R.id.tvDate); 


    final Calendar c = Calendar.getInstance(); 
    yy = c.get(Calendar.YEAR); 
    mm = c.get(Calendar.MONTH); 
    dd = c.get(Calendar.DAY_OF_MONTH); 

    // set current date into textview 
    tvDisplayDate.setText(new StringBuilder() 
    // Month is 0 based, just add 1 
      .append(yy).append(" ").append("-").append(mm + 1).append("-") 
      .append(dd)); 
2
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); 
String currentDateandTime = sdf.format(new Date()); 
tv.setText(currentDateandTime); 

dies ist die Art und Weise, wie ich es tun

1

Anmerkung *:

import java.text.DateFormat; 
import java.util.Date; 

// Donot import "import java.sql.Date;" 

TextView tv = (TextView) findViewById(R.id.textView1); 
String ct = DateFormat.getDateInstance().format(new Date()); 
tv.setText(ct); 
1

, wenn Sie einen Toast generieren möchten, die auf eine Schaltfläche klicken aktuelle Uhrzeit angezeigt wird dann diesen Code verwenden.

bt.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Calendar c = Calendar.getInstance(); 
      hour = c.get(Calendar.HOUR_OF_DAY); 
      min = c.get(Calendar.MINUTE); 
      int ds = c.get(Calendar.AM_PM); 
      if(ds==0) 
      AM_PM="pm"; 
      else 
      AM_PM="am"; 

      Toast.makeText(MainActivity.this, ""+hour+":"+min+AM_PM, Toast.LENGTH_SHORT).show(); 


     } 
    }); 
0
Calendar c = Calendar.getInstance(); 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String formattedDate = df.format(c.getTime()); 
System.out.println("Currrent Date Time : "+formattedDate); 
1

Betrachten wir ein TextClock mit, wie es Updates für Sie behandelt. Für weitere Informationen hier ist die Dokumentation Docs. AnalogClock und DigitalClock kann auch nützlich sein, wenn Ihr Ziel nur ist aktuelle Uhrzeit und das Datum

<TextClock 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="30sp" 
    android:format24Hour="MMM,yyyy\n\thh:mm"/>