10

我不知道下面的代码有什么问题 -

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

}
4

10 回答 10

30

我建议:

long date = System.currentTimeMillis(); 

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

它以以下示例格式显示

Mon Jan 5, 2009 4:55 PM

您可以使用任何您想要的格式 - 选项可以在 Oracle中找到

于 2012-10-17T12:49:13.487 回答
15

使用此代码:

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));
于 2012-10-17T12:56:56.640 回答
5

以下解决方案可能会有所帮助 -

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(" "));
}
于 2012-10-17T12:50:37.840 回答
3

考虑使用 TextClock,因为它会为您处理更新。有关更多信息,请参阅文档Docs如果您的目标只是显示当前时间和日期, AnalogClockDigitalClock也很有用

 <TextClock
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:format24Hour="MMM,yyyy\n\thh:mm"/>
于 2017-02-22T20:25:32.127 回答
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);
于 2012-10-17T12:49:41.023 回答
2
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());
tv.setText(currentDateandTime);

我就是这样做的

于 2012-10-17T12:59:16.673 回答
1

笔记*:

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);
于 2013-04-09T03:45:47.810 回答
1

如果您想生成一个在按钮单击时显示当前时间的 toast,请使用此代码。

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


        }
    });
于 2013-09-27T06:00:37.480 回答
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);
于 2016-08-04T05:34:27.447 回答
-1
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
String currentDateandTime = sdf.format(new Date());    
tv.setText(currentDateandTime);
于 2019-10-16T06:57:03.427 回答