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.widget.TextView;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {

        TextView cal = (TextView)findViewById(;
        Calendar calendar = Calendar.getInstance(); 

         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