How To Become A Programmer Without A Degree & Experience

Java (Android Studio) Tutorial – Calendar, SimpleDateFormat –

Java (Android Studio) Tutorial – Calendar, SimpleDateFormat –

Java (Android Studio) Tutorial  - Calendar, SimpleDateFormat -

 

 

Introduction

In the video, I introduced how to use Calendar and SimpleDateFormat by creating simple app which displays the current date.

You can watch each step in the video, so try and enjoy coding!

 
 

Environment

Android Studio 2.1.1
Simulator (Genymotion)

 
 

Sample Code

package practice.myapplication;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
 
public class main extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
 
        TextView yLabel = (TextView)findViewById(R.id.yLabel);
        TextView mLabel = (TextView)findViewById(R.id.mLabel);
        TextView dLabel = (TextView)findViewById(R.id.dLabel);
        TextView eLabel = (TextView)findViewById(R.id.eLabel);
 
 
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MMMM/d/E", Locale.US); // Set your locale!
        String strDate = sdf.format(cal.getTime());
 
        String[] values = strDate.split("/",0);
 
        // Confirm
//        for (int i = 0; i < values.length; i++) {
//            Log.v("CHECK_DATE", values[i]);
//        }
 
        yLabel.setText(values[0]);
        mLabel.setText(values[1]);
        dLabel.setText(values[2]);
        eLabel.setText(values[3]);
 
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="practice.myapplication.main"
    android:background="#3fb399"
    android:orientation="vertical"
    android:gravity="center">
 
    <TextView
        android:id="@+id/yLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2016"
        android:textSize="24sp"
        android:textColor="#ffffff"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="-40dp"/>
 
 
    <TextView
        android:id="@+id/mLabel"
        android:layout_width="220dp"
        android:layout_height="50dp"
        android:text="July"
        android:textSize="24sp"
        android:textColor="#ffffff"
        android:background="#206757"
        android:gravity="center"
        android:layout_marginTop="40dp"/>
 
 
    <TextView
        android:id="@+id/dLabel"
        android:layout_width="220dp"
        android:layout_height="200dp"
        android:text="3"
        android:textSize="56sp"
        android:textColor="#206757"
        android:background="#ffffff"
        android:gravity="center"/>
 
 
    <TextView
        android:id="@+id/eLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sun"
        android:textSize="18sp"
        android:textColor="#206757"
        android:layout_marginTop="-40dp"
        android:layout_marginLeft="80dp"/>
 
</LinearLayout>