How do I display the current date and time in the text viewer inside Fragment?

advertisements

I would like to display current Date and time in a Textview in my app fragments, in the Format : DD/MM/YEAR:H:Min but I have no idea how to code this up as I am new to programming. Where/What to begin with?

my current code :

package com.example.myshops;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Fragment;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Monday_fragment extends Fragment implements OnClickListener { private TextView datetimetext;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.item1_fragment, container, false);

    Button nextpageButton = (Button) v.findViewById(R.id.Item2Button);
    nextpageButton.setOnClickListener((OnClickListener) getActivity());
    return v;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.nextpageButton:

        break;
    }

    datetimetext.setText("" + DateFormat.format("dd/MM/yyyy kk:mm", System.currentTimeMillis()));

    //---Inflate the layout for this fragment---
//return inflater.inflate( R.layout.item1_fragment, container, false);

}

}


Okay, not that hard as there are several methods to do this.

String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

// textView is the TextView view that should display it
textView.setText(currentDateTimeString);

There is more to read in the documentation that can easily be found here http://developer.android.com/reference/java/text/DateFormat.html#getDateTimeInstance%28%29. There you'll find more information on how to change the format used for conversion.