Thursday, 28 August 2014

Display formatted date/time using String.format()

In this post we explains how to display formatted calendar using String.format().Let's hae a look.
Download and use...
 
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 TextView cal = (TextView)findViewById(R.id.viewdate);
         
	        Calendar calendar = Calendar.getInstance();
	 
	        cal.setText(
	         String.format("%td-%tm-%tY\n", calendar, calendar, calendar) +
	         String.format("%ty/%tm/%td\n", calendar, calendar, calendar) +
	         String.format("%tI:%tM:%tS %tp\n", calendar, calendar, calendar, calendar) +
	         "\n" +
	         String.format("%tD\n", calendar) +
	         String.format("%tc\n", calendar) +
	         String.format("%tF\n", calendar) +
	         String.format("%tr\n", calendar)
	        );
	}
	
}

No comments:

Post a Comment