Get Display current time in 12 hour format with AM-PM in android

Programmatically access 12 hours time format time dynamically using coding in android activity.

12 hours time format is the most popular time format for all over world because most of the companies, online websites supports 12 hours time format because it can display AM and PM with time format. So here is the complete step by step tutorial for Get Display current time in 12 hour format with AM-PM in android.

android-project-download-code-button

How to Get Display current time in 12 hour format with AM-PM in android.

Code for MainActivity.java file.

 package com.currenttime12hoursformat_android_examples.com;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 TextView DisplayTimeIn12format;
 Button AccessTime;
 SimpleDateFormat simpleDateFormat;
 String time;
 Calendar calander;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 DisplayTimeIn12format = (TextView)findViewById(R.id.textView1);
 AccessTime = (Button)findViewById(R.id.button1);
 
 calander = Calendar.getInstance();
 simpleDateFormat = new SimpleDateFormat("hh:mm:ss a");
 
 time = simpleDateFormat.format(calander.getTime());
 
 AccessTime.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 DisplayTimeIn12format.setText(time);
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.currenttime12hoursformat_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="175dp"
 android:text="Time display here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="33dp"
 android:text="Get Display current time in 12 hour format with AM/PM" />

</RelativeLayout>

Screenshot:

Get Display current time in 12 hour format with AM-PM in android

Click here to download Get Display current time in 12 hour format with AM-PM in android project.

Leave a Reply

Your email address will not be published. Required fields are marked *