2013-04-25 49 views
0

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

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

你可以手动指定值ex:如果1 “星期日”,并设置它的TextView更好地利用Weekoftheday代替MONTHDAY – vinoth 2013-04-25 06:31:09

+0

如果你想要将一个整数转换为字符串意味着:'value。tostring' – Subburaj 2013-04-25 06:33:58

回答

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]; 
} 
+0

我得到一个IndexOutOfBoundsException ... – ThatGuyThere 2013-04-26 02:39:44

+0

@ThatGuy然后使用'weekno-1' – Pragnani 2013-04-26 04:34:20

+0

@ThatGuyThere让我知道是否有任何其他问题 – Pragnani 2013-04-26 15:37:10

1

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

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

当我尝试使用它时,它总是告诉我“Date()未定义”,并要求我在括号之间插入一个数字。我不确定如何真正使TextView使用这种格式... – ThatGuyThere 2013-04-26 02:56:36

+0

如果您尚未导入Date类,则使用java.util.Date()更新答案。 – 2013-05-21 15:24:44

0

你的意思是显示为MonTue,Wed,....? 使用此格式。

SimpleDateFormat curFormatDate = new SimpleDateFormat("EEE"); 
+0

MMM用于“Jan”,“Feb”,“Mar”月;平日应该是新的SimpleDateFormat(“EEE”); – Raghuram 2015-04-07 14:16:14

3

要格式化时间:

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

它在本周返回一天的名称(周日,周五..) - 见description of format string(这是一个PHP手册页,但这些符号是相同的,这是很好aranged)

为了使textViews每秒更新一次,您必须使用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); 
+0

感谢您指出格式字符串(PHP页面)。这实际上是我所需要的,因为我使用Time not Date或Calendar进行操作。 – 2014-05-10 10:09:20