SLASHxWYLDE
SLASHxWYLDE

Reputation: 1

how to subtract fixed number of days from a provided date by the user in android studio?

e.g. if the user fills in the date and there is a spinner consisting of three options i.e. 70,90 & 45. So if the user fills in the date and selects for example 70 & clicks submit then 20 days should be subtracted from the date filled by the user and the result should be shown i.e. the result will the date 20 days before the date filled in by the user. If selects 90 then 30 days before and so on... (java code) the ui image

<Button
    android:id="@+id/backBtn7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Back"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.915" />

<EditText
    android:id="@+id/input1"
    android:layout_width="222dp"
    android:layout_height="49dp"
    android:ems="10"
    android:hint="Shipment Date"
    android:inputType="date"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.532"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.096" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="220dp"
    android:layout_height="47dp"
    android:text="Lead Time"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.529"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/input1"
    app:layout_constraintVertical_bias="0.063"
    tools:ignore="HardcodedText" />

<Button
    android:id="@+id/submitBtn2"
    android:layout_width="223dp"
    android:layout_height="53dp"
    android:text="Submit"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.539"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/spinner1"
    app:layout_constraintVertical_bias="0.074" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/tv_result"
        android:layout_centerInParent="true" />
</RelativeLayout>

Java Code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_p_c_d_calculator);
    backBtn7 = findViewById(R.id.backBtn7);
    submitBtn2 = findViewById(R.id.submitBtn2);
    getSupportActionBar().setTitle("PCD Calculator");
    tv_result = (TextView) findViewById(R.id.tv_result);
    input1 = findViewById(R.id.input1);
    spinner1 = findViewById(R.id.spinner1);
    submitBtn2 = findViewById(R.id.submitBtn2);


    submitBtn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });



    tv_result.setText("PCD: ");


    String[] users = { "70", "90", "45" };
    Spinner spin = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, users);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setPrompt("Lead Time");
    spin.setAdapter(adapter);


    backBtn7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(),MainActivity.class));
            finish();
        }
    });
    

}

}

Upvotes: 0

Views: 159

Answers (1)

OLezhko
OLezhko

Reputation: 150

LocalDate#minusDays

You can use LocalDate from the java.time package.

You can create LocalDate instance by using LocalDate#of static method:

    LocalDate date = LocalDate.of(2021, 4, 5); // Apr 5, 2021

If your date is input as a String, you can use LocalDate#parse method with DateTimeFormatter, for example:

    LocalDate date = LocalDate.parse("2021/04/05", DateTimeFormatter.ofPattern("yyyy/MM/dd")); // Apr 5, 2021

And finally, having LocalDate instance you can use a good bunch of methods for manipulations with dates:

    LocalDate dateMinus30Days = date.minusDays(30);

For details, you can have a look at docs or, for example, at this tutorial.

Upvotes: 1

Related Questions