Blog purpose for android basic example for android app developer. any query please my contact

Sunday, 16 January 2022

Android studio current date and time

Android studio current date and time


today learn the tutorial android studio current date and time follow this android studio current date and time example.


1.MainActivity.java:


import android.annotation.TargetApi;

import android.os.Build;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.widget.TextView;

import java.text.SimpleDateFormat;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

   @TargetApi(Build.VERSION_CODES.O)

   @Override

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      TextView textView=findViewById(R.id.date);

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");

      String currentDateandTime = sdf.format(new Date());

      textView.setText(currentDateandTime);

   }

}


2.activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:app="http://schemas.android.com/apk/res-auto"

   xmlns:tools="http://schemas.android.com/tools"

   android:layout_width="match_parent"

   android:gravity="center"

   android:layout_height="match_parent">

   <TextView

      android:id="@+id/date"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_centerHorizontal="true"

      android:textSize="30sp"

      android:layout_marginBottom="36dp" />

</RelativeLayout>


No comments:

Post a Comment