Пишу программку, которая должна выводить дату на несколько дней позже, исходя из выбранной на объекте CalendarView. Получился такой код:
package com.example.beecalc.ui.gallery;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.TextView;
import java.text.ParseException;
import java.util.Calendar;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import java.util.GregorianCalendar;
import com.example.beecalc.R;
import java.lang.String;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class GalleryFragment extends Fragment {
CalendarView cal;
private GalleryViewModel galleryViewModel;
public View onCreateView(@NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
textView.setText("Дата готовых маток:");
final TextView nach = (TextView) root.findViewById(R.id.v1);
final TextView x1 = (TextView) root.findViewById(R.id.v2);
final TextView x2 = (TextView) root.findViewById(R.id.v3);
final CalendarView c = root.findViewById(R.id.calendarView);
c.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int day) {
String a = String.format("%02d.%02d.%04d", day, month + 1, month);
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(a));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DATE, 40);
String output = sdf1.format(c.getTime());
nach.setText(output);
}
Проблема в том, что дата (измененная) выводится только на сегодняшний день, какая бы дата не выбиралась (при выборе любой даты выводится измененная сегодняшняя). В Java не так долго, не могу разобраться, в чем проблема... Прошу помощи, спасибо заранее!