I'm working with a calendar and a NoSQL database that stores data whenever a user clicks on a specific date in the calendar.
I'm currently storing data using nodes in format of "YYYYMMDD"
e.g. -
20190823:
generatedKeyofUser: <user's data>
20190824:
generatedKeyofAnotherUser: <another's data>
I wish to be able to fetch not only the date the user has clicked on but also up to 4 days prior.
for example, if the user clicked on Jan 2nd 2019, I wish to query the database with the following dates "20190102", "20190101, "20181231", "20181230"
Currently I'm constructing a string in said format like so -
calendarView.setOnDayClickListener(event-> {
Calendar clickedDate = event.getCalendar();
Intent intent = new Intent(CalendarActivity.this, DateActivity.class);
SharedPreferences.Editor editor = sp.edit();
String year = Integer.toString(clickedDate.get(Calendar.YEAR));
String month = new DecimalFormat("00").format(clickedDate.get(Calendar.MONTH) + 1);
String day = new DecimalFormat("00").format(clickedDate.get(Calendar.DAY_OF_MONTH));
editor.putString("year", year);
editor.putString("month", month);
editor.putString("day",day );
editor.apply();
startActivity(intent);
});
sending it to the other activity so it can fetch the user's data from the database.
Edit: I edited my code as shown below -
calendarView.setOnDayClickListener(event-> {
Intent intent = new Intent(CalendarActivity.this, DateActivity.class);
SharedPreferences.Editor editor = sp.edit();
// Chosen date
Calendar clickedDate = event.getCalendar();
// Yesterday's chosen date
Calendar clickedDateYesterday = (Calendar) clickedDate.clone();
clickedDateYesterday.add(Calendar.DAY_OF_YEAR, -1);
// Two days ago from chosen date
Calendar clickedDateTwoDaysAgo = (Calendar) clickedDate.clone();
clickedDateTwoDaysAgo.add(Calendar.DAY_OF_YEAR, -2);
// Three days ago from chosen date
Calendar clickedDateThreeDaysAgo = (Calendar) clickedDate.clone();
clickedDateYesterday.add(Calendar.DAY_OF_YEAR, -3);
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String formattedChosenDate = formatter.format(clickedDate.getTime());
String formattedYesterdayChosenDate = formatter.format(clickedDateYesterday.getTime());
String formattedTwoDaysAgoChosenDate = formatter.format(clickedDateTwoDaysAgo.getTime());
String formattedThreeDaysAgoChosenDate = formatter.format(clickedDateThreeDaysAgo.getTime());
Log.i("dates", "Chosen Date - " + formattedChosenDate + " ; Yesterday - " + formattedYesterdayChosenDate + " ; Two Days Ago - "+
formattedTwoDaysAgoChosenDate + " ; Three Days Ago - " + formattedThreeDaysAgoChosenDate);
editor.apply();
startActivity(intent);
});
But the Log shows
Chosen Date - 20190808 ; Yesterday - 20190804 ; Two Days Ago - 20190806 ; Three Days Ago - 20190808
instead of
Chosen Date - 20190808 ; Yesterday - 20190807 ; Two Days Ago - 20190806 ; Three Days Ago - 20190805

add(), provideCalendar.DAY_OF_MONTHand pass -1. This code decrements it by 1.java.util.Date,java.util.Calendar, &java.text.SimpleDateFormatare now legacy, supplanted by the java.time classes. Most java.time functionality is back-ported to Java 6 & Java 7 in the ThreeTen-Backport project. Further adapted for earlier Android (<26) in ThreeTenABP. See How to use ThreeTenABP….