[embedyt] https://www.youtube.com/watch?v=2DYavHADmB8[/embedyt]
activity_main.xmlMainActivity.java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context="com.codinginflow.futuredateexample.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="getDate" android:text="Get date" /> <TextView android:id="@+id/text_view_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Today: \nFuture:" android:textSize="16sp" /> </LinearLayout>
package com.codinginflow.futuredateexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import java.util.Calendar; import java.util.Date; public class MainActivity extends AppCompatActivity { private TextView textViewDate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textViewDate = findViewById(R.id.text_view_date); } public void getDate(View v) { Calendar c = Calendar.getInstance(); Date today = c.getTime(); c.add(Calendar.DATE, 3); c.add(Calendar.MONTH, 1); c.add(Calendar.YEAR, 1); Date future = c.getTime(); textViewDate.setText("Today: " + today + "\n" + "Future: " + future); } }