Contoh kode sederhana untuk mangambil tanggal terkini dari system android

Berikut kode untuk mangambil tanggal terkini dari system android dan menampilkannya di dalam edit text.

File MainActivity.java

package com.example.test;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class PenjualanTambah extends Activity {
	
	EditText tanggal;
	int mYear,mMonth,mDay;
	
	@SuppressLint("SimpleDateFormat") @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.penjualantambah);
		
		tanggal = (EditText) findViewById(R.id.et_id_tanggal);

		Calendar c=Calendar.getInstance();
        mYear=c.get(Calendar.YEAR);
        mMonth=c.get(Calendar.MONTH);
        mDay=c.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        tanggal.setText( sdf.format(c.getTime()));

	}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s