Android Newbie Android Newbie - 7 months ago 49
Java Question

Setting date to tabs

I'm trying to get the current date to my tabs but the date to the left is not decreasing by one. What am i doing wrong? Any help would be appreciated, thanks.

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
Calendar cal = Calendar.getInstance();
DateFormat dateFormat1 = new SimpleDateFormat("EEEE, MMM dd, yyyy", Locale.getDefault());


for (int j = 0; j < 2; j++){
cal.add(Calendar.DATE, -1);
String yest = dateFormat1.format(cal.getTime());
adapter.addFragment(new ThreeFragment(), yest);
}

String todate = dateFormat1.format(cal.getTime());
adapter.addFragment(new OneFragment(), todate);

for(int i = 0; i < 2; i++) {
cal.add(Calendar.DATE, 1);
String tomo = dateFormat1.format(cal.getTime());
adapter.addFragment(new TwoFragment(), tomo);

}

viewPager.setAdapter(adapter);
}


enter image description here

Answer

Incorrect logic

The logic behind your code is incorrect. Today is the 30th, for example. At the point where your code gets to "today", you have already deleted two days from it. So your code makes it look like "today" is the 28th.

Furthermore, between those two loops, where you generate todate you do not call add with -1, so the date remains the same as the last time through the first loop.

I copied your code, replacing the ViewPageAdapter with System.out.println calls.

Calendar cal = Calendar.getInstance ();
DateFormat dateFormat1 = new SimpleDateFormat ( "EEEE, MMM dd, yyyy" , Locale.getDefault () );
System.out.println ( "cal: " + dateFormat1.format ( cal.getTime () ) );

for ( int j = 0 ; j < 2 ; j ++ ) {
    cal.add ( Calendar.DAY_OF_MONTH , -1 );
    String yest = dateFormat1.format ( cal.getTime () );
    System.out.println ( "yest: " + yest );
}

String todate = dateFormat1.format ( cal.getTime () );
System.out.println ( "todate: " + todate );

for ( int i = 0 ; i < 2 ; i ++ ) {
    cal.add ( Calendar.DATE , 1 );
    String tomo = dateFormat1.format ( cal.getTime () );
    System.out.println ( "tomo: " + tomo );
}

When run.

cal: Saturday, Apr 30, 2016
yest: Friday, Apr 29, 2016
yest: Thursday, Apr 28, 2016
todate: Thursday, Apr 28, 2016
tomo: Friday, Apr 29, 2016
tomo: Saturday, Apr 30, 2016

Legacy classes

You are using old legacy date-time classes that have proven to be poorly designed, confusing, and troublesome. They are also mutable, contributing to the flaws in your code’s logic.

java.time

The old classes have been supplanted by the java.time framework built into Java 8 and later. Much of that functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project, and further adapted for Android in the ThreeTenABP project.

The java.time classes use the Immutable Object pattern. Rather than change (“mutate”) the object, a new object is created based on the original’s value.

For date-only, without time-of-day and without time zone, use LocalDate.

Another issue is time zone. Your code ignores this crucial issue. Determining “today” requires a time zone, as the date various around the globe. If omitted, the JVM’s current default time zone is applied. Better to be explicit. Ditto for Locale, as seen in code below, better to be explicit than rely on JVM’s current default.

Here is my version of the code, without the loops.

Tip: Usually best to get the business objects built and organized first (LocalDate objects here), kept separate from the presentation (String values for display).

ZoneId zoneId = ZoneId.of ( "America/Montreal" );
LocalDate today = LocalDate.now ( zoneId );

LocalDate yesterday = today.minusDays ( 1 );
LocalDate dayBeforeYesterday = yesterday.minusDays ( 1 );

LocalDate tomorrow = today.plusDays ( 1 );
LocalDate dayAfterTomorrow = tomorrow.plusDays ( 1 );

List<String> days = new ArrayList<> ( 5 );
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate ( FormatStyle.MEDIUM );
formatter = formatter.withLocale ( Locale.US );  // Or Locale.CANADA, LOCALE.CANADA_FRENCH.
days.add ( dayBeforeYesterday.format ( formatter ) );
days.add ( yesterday.format ( formatter ) );
days.add ( today.format ( formatter ) );
days.add ( tomorrow.format ( formatter ) );
days.add ( dayAfterTomorrow.format ( formatter ) );

System.out.println ( "today: " + today );
System.out.println ( "days: " + days );

When run.

today: 2016-04-30
days: [Apr 28, 2016, Apr 29, 2016, Apr 30, 2016, May 1, 2016, May 2, 2016]