0

我有一个文本视图,它将星期几显示为整数(0-7)。我希望它可以将其转换为字符串,然后可以在 TextView 中显示。我的代码如下。另外,我怎样才能让 TextViews 更新时间、日期等(它只显示应用程序打开的时间)?提前致谢。

MainActivity.java:

package press.linx.calendar;

import java.sql.Date;
import java.text.SimpleDateFormat;

import android.os.Bundle;
import android.app.Activity;
import android.text.format.Time;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView day = (TextView)findViewById(R.id.day);
    TextView month = (TextView)findViewById(R.id.month);
    TextView year = (TextView)findViewById(R.id.year);
    TextView time = (TextView)findViewById(R.id.time);


    Time today = new Time(Time.getCurrentTimezone());
    today.setToNow();

    day.setText("" + today.monthDay);             // Day of the month (0-31)
    month.setText("" + today.month);              // Month (0-11)
    year.setText("" + today.year);                // Year 
    time.setText("" + today.format("%k:%M"));  // Current time


}

}

更新:我使用这段代码得到了它:

final Calendar calendar = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("MMM"); // 3-letter month name & 2-char day of month
    TextView datetxt = (TextView) findViewById(R.id.nameofyourtextview);
    datetxt.setText(formatter.format(calendar.getTime()));
4

5 回答 5

3

要格式化您的时间:

Time time = new Time();
time.format("%A");

它返回星期几的名称(星期日,星期五..) - 请参阅格式字符串的描述(这是一个 PHP 手册页,但符号相同且排列整齐)

为了使 textViews 每秒更新一次,您必须使用TimerTimerTask.

定义更新时间任务:

class UpdateTimeTask extends TimerTask {

   public void run() {
       // Update time, must be called using runOnUiThread
   }
}

然后设置计时器:

Timer timer = new Timer();
TimerTask updateTime = new UpdateTimeTask();
timer.scheduleAtFixedRate(updateTime, 0, 1000);
于 2013-04-25T06:39:28.917 回答
1

要获取一周中的当前日期(即星期一、星期二、星期三等),请尝试:

DateFormat fmt = new SimpleDateFormat( "EEEE" );
fmt.format( new java.util.Date() );
于 2013-04-25T06:33:39.507 回答
1

我假设您正在寻找以以下格式显示的日期。

您可以使用以下

Date now = new Date();
SimpleDateFormat dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
System.out.println("Format :   " + dateFormatter.format(now));

输出

 Format :   Thursday, April 25, 2013

一些有用的链接

http://www.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html

http://www.roseindia.net/tutorial/java/core/convertDateToWords.html

于 2013-04-25T06:33:39.783 回答
0

你的意思是显示为Mon, Tue, Wed,.... 吗?使用这种格式。

SimpleDateFormat curFormatDate = new SimpleDateFormat("EEE"); 
于 2013-04-25T06:34:29.433 回答
0

试试这个

  month.setText(getMonth(today.month));    
  day.setText(getWeek(today.monthDay));  

根据月份数获取月份的方法

public String getMonth(int month) {
    return new DateFormatSymbols().getMonths()[month];
}

根据周数获取周的方法

public String getWeek(int weekno) {
    return new DateFormatSymbols().getWeekdays()[weekno];
}
于 2013-04-25T06:38:25.067 回答