Android Date And Time Formats Conversion


Android Date And Time Formats Conversion



1.XML File

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.logics.tutorials.MainActivity">

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:text="" />
</RelativeLayout>

2. Activity File

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    TextView dateTextView;

    
    String finalSecondDate,finalThirdDate,finalFourthDateTime,finalFifthDate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dateTextView = (TextView) findViewById(R.id.dateTextView);

        /**********----------- first method ----------*********/
        //This is a simple Date class prints
        Date date=new Date();
        Log.e("date", String.valueOf(date));
        // output = Sat Dec 16 10:16:55 GMT+05:30 2017

        /**********----------- first method closed ------------------*********/



        /***********--------------------- Second Method ------------------------***********/
        String secondDate="17-09-2017"; //simple date in String
        //pass the date format
        SimpleDateFormat secondSimpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date dateTwo = null;
        try {
            dateTwo = secondSimpleDateFormat.parse(secondDate);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd, yyyy");
            finalSecondDate = simpleDateFormat.format(dateTwo);
            Log.e("finalSecondDate:",finalSecondDate);

            //output   Sep 17, 2017

            ///// Here MMM We Can Use
            /**
             * M - 9

             MM - 09

             MMM - Sep

             MMMM - September

             if MMMM then output will be
             output = September 17, 2017

             */

         } catch (ParseException e) {
            e.printStackTrace();
        }

        /***********------------ Second Method Closed ------------------------***********/


        /***********--------------------- Third Method ----------------------***********/
        String thirdDate="Sep 17, 2017";
        SimpleDateFormat thirdDateFormat = new SimpleDateFormat("MMM dd, yyyy");
        Date dateThree = null;
        try {
            dateThree = thirdDateFormat.parse(thirdDate);

            //this format what we want to convert from above format
            SimpleDateFormat simpleDateFormatThird = new SimpleDateFormat("dd-MM-yyyy");
            finalThirdDate = simpleDateFormatThird.format(dateThree);

            Log.e("finalThirdDate:",finalThirdDate);

            //output = 17-09-2017

        } catch (ParseException e) {
            e.printStackTrace();
        }
      


        /***********  Getting Only Time Fourth Method  ***********/

        //if we have this type of date string and we want to get only time then we use...
        String fourthDateTime="Sep 17, 2017 10:16:55";

        SimpleDateFormat fourthDateTimeFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");

        Date dateFourth = null;
        try {
            //pass here
            dateFourth = fourthDateTimeFormat.parse(fourthDateTime);
            SimpleDateFormat simpleDateFormatFourth = new SimpleDateFormat("HH:mm a");
            finalFourthDateTime = simpleDateFormatFourth.format(dateFourth);
            Log.e("finalFourthDateTime:",finalFourthDateTime);

            //output =  10:16 am
            // here a is used for shows am or pm with time
           

        } catch (ParseException e) {
            e.printStackTrace();
        }
        



        /***********---------------------Getting Name Of Day Fifth Method   ***********/

        

        String fifthDate="17-09-2017";

        SimpleDateFormat fifthDateFormat = new SimpleDateFormat("dd-MM-yyyy");

        Date dateFifth = null;

        try {
            dateFifth = fifthDateFormat.parse(fifthDate);

            SimpleDateFormat simpleDateFormatFifth = new SimpleDateFormat("EEEE, MMM d, yyyy");
              finalFifthDate = simpleDateFormatFifth.format(dateFifth);
            Log.e("finalFifthDate:",finalFifthDate);
            //Output = Sunday, Sep 17, 2017
            // if we Want to show only Sun instead of Sunday the we use EEE

        } catch (ParseException e) {
            e.printStackTrace();
        }
        /***********----------------- Fifth Method Closed -----------------------------------***********/



        // just show output Strings in TextView and Use \n for new Line
        dateTextView.setText("1. Date: " + String.valueOf(date) + " \n\n" + "2. Get Name Of Month : "+ finalSecondDate + " \n\n" +
        "3. Date Format : "+finalThirdDate  + " \n\n" + "4. Get Time from Date and Time Format : "+finalFourthDateTime + " \n\n"  + "5.  Name Of Day from Date : " + finalFifthDate);


    }
}





Output









Comments

Popular Posts