0

I want to take today's date and change it to tomorrow's date. I will explain:

Today is the 14th of this month. I want to show tomorrow's number (15th) automatically. How can I do it?

I tried to convert string to integer and after that add 1 but i could not run the program....

package com.example.changetime;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView dateOne = findViewById(R.id.dateOne);
        TextView dateTwo = findViewById(R.id.dateTwo);

        String currentDate = new SimpleDateFormat("dd-MM-yy", Locale.getDefault()).format(new Date());
        dateOne.setText(currentDate);

        String currentDay = new SimpleDateFormat("dd", Locale.getDefault()).format(new Date());
        dateTwo.setText(currentDay); //Here I want to show tomorrow's date

    }
}

Thank you for the attention!

JackCohen
  • 1
  • 2
  • Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Oct 14 '21 at 20:45
  • Under the linked original question I recommend [the answer by Basil Bourque](https://stackoverflow.com/a/23004726/5772882) using java.time. You may also find [the one by catch23](https://stackoverflow.com/a/30024286/5772882) interesting. – Ole V.V. Oct 14 '21 at 20:48

1 Answers1

1

If you are Java 8+ you can use the LocalDate and DateTimeFormatterclasses from the java.time library instead of SimpleDateFormat:

String tomorrow = LocalDate.now().plusDays(1).format(DateTimeFormatter.ofPattern("dd-MM-yy"));
System.out.println(tomorrow);

Note that .now() gets current date, .plusDays(1) adds a single day and .format formats the String into the given pattern.

Output (on October 14th 2021):

15-10-21
Nexevis
  • 4,647
  • 3
  • 13
  • 22