Aug 5, 2014

Display formatted date/time using String.format()

Examples of using String.format() to display formatted calendar:


package com.example.androidtime;

import java.util.Calendar;

import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView cal = (TextView)findViewById(R.id.cal);
        
        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)
        );

    }

}

The layout file is same as last post.

No comments:

Post a Comment

Infolinks In Text Ads