0

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()));
ThatGuyThere
  • 365
  • 2
  • 10
  • 23

5 Answers5

3

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);
lopisan
  • 7,720
  • 3
  • 37
  • 45
  • Thanks for pointing out the format strings (the PHP page). This is actually what I need as I manipulate with Time not Date or Calendar at all. – Thuy Trinh May 10 '14 at 10:09
1

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() );
Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • When I try to use this, it always tells me "Date() is undefined" and requires me to put a number between the parentheses. I'm not sure how to actually make a TextView use this format... – ThatGuyThere Apr 26 '13 at 02:56
  • The answer has been updated use `java.util.Date()` if you don't already import the `Date` class. – Mark Allison May 21 '13 at 15:24
1

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

0

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

SimpleDateFormat curFormatDate = new SimpleDateFormat("EEE"); 
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
elL
  • 767
  • 2
  • 14
  • 35
0

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];
}
Pragnani
  • 20,075
  • 6
  • 49
  • 74