Have TextView show day of week as letters not inte

2019-09-02 20:26发布

I have a textview which shows the day of the week as an integer (0-7). I would prefer if it could convert that to a string, which could then be shown in a TextView. My code is below. Also, how can I make it so the TextViews update the time, date, etc. (it only shows the time the app is opened)? Thanks in advance.

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


}

}

UPDATE: I got it using this piece of code:

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条回答
迷人小祖宗
2楼-- · 2019-09-02 20:32

To get the current day of the week (i.e. Monday, Tuesday, Wednesday, etc.) try:

DateFormat fmt = new SimpleDateFormat( "EEEE" );
fmt.format( new java.util.Date() );
查看更多
我命由我不由天
3楼-- · 2019-09-02 20:38

Do you mean display as Mon, Tue, Wed,.... ? Use this format.

SimpleDateFormat curFormatDate = new SimpleDateFormat("EEE"); 
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-09-02 20:39

Try this

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

method to get month based on month number

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

method to get week based on week number

public String getWeek(int weekno) {
    return new DateFormatSymbols().getWeekdays()[weekno];
}
查看更多
放荡不羁爱自由
5楼-- · 2019-09-02 20:40

To format your Time:

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

It returns name of day in week (Sunday, Friday..) - see description of format string (It's a PHP man page, but the symbols are same and it's well-aranged)

In order to make textViews updated every second you have to use Timer and TimerTask.

Define UpdateTimeTask:

class UpdateTimeTask extends TimerTask {

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

and then set timer:

Timer timer = new Timer();
TimerTask updateTime = new UpdateTimeTask();
timer.scheduleAtFixedRate(updateTime, 0, 1000);
查看更多
Rolldiameter
6楼-- · 2019-09-02 20:44

I assume you are looking for the date to be displayed in the below format.

You can use the below

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

Output

 Format :   Thursday, April 25, 2013

Few helpful links

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

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

查看更多
登录 后发表回答