0
package pratyush.grapudai;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends ActionBarActivity {

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

     public void SubmitOrder(View view) {

        printStandardDate(Date);
    }


    private String printStandardDate(Date date) {
        return new SimpleDateFormat("dd/MM/yy HH:mm").format(date);
    }

}

Actually I want it to show date when a button is pressed. Like using a function public void ButtonPressed(View view) { } Is there any other better possible code?

aga
  • 27,954
  • 13
  • 86
  • 121

1 Answers1

0

You may try one of these alternative ways,

1. Date() + SimpleDateFormat()

 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    System.out.println(dateFormat.format(date)); //2014/08/06 15:59:48

2. Calender() + SimpleDateFormat()

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); //2014/08/06 16:00:22
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50