让TextView显示星期几作为字母不是整数

时间:2013-04-25 06:24:13

标签: android time textview

我有一个textview,它将星期几显示为整数(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()));

5 个答案:

答案 0 :(得分:3)

格式化您的时间:

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

它返回星期几的名称(星期日,星期五......) - 请参阅description of format string(这是一个PHP手册页,但符号相同,并且已经很好地安排了)

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

定义UpdateTimeTask:

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

答案 1 :(得分:1)

要获取当周的当天(即周一,周二,周三等),请尝试:

DateFormat fmt = new SimpleDateFormat( "EEEE" );
fmt.format( new java.util.Date() );

答案 2 :(得分: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

答案 3 :(得分:0)

您的意思是显示为MonTueWed,.... 使用此格式。

SimpleDateFormat curFormatDate = new SimpleDateFormat("EEE"); 

答案 4 :(得分: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];
}