0

guys actually my logic is that I want to basically show a current month with the last three months in tab layout which I am using to show the months wise data to the user so if present its "June" so I want to show tabs "April, May, June" these 3 months data to the user.

Saif Khan
  • 116
  • 8

2 Answers2

0

Okay here is code for what you wants

For JAVA:

Calendar c = new GregorianCalendar();
c.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("MMMM"); //add space after MMMM and add yyyy for year
Log.d("NOW ", sdf.format(c.getTime()));   // NOW
c.add(Calendar.MONTH, -1);
Log.d("One month ago ", sdf.format(c.getTime()));   // One month ago
c.add(Calendar.MONTH, -1);
Log.d("Two month ago ", sdf.format(c.getTime()));   // Two month ago

For KOTLIN:

val c: Calendar = GregorianCalendar()
        c.setTime(Date())
        val sdf = SimpleDateFormat("MMMM") //add space after MMMM and add yyyy for year

        Log.d("NOW ", sdf.format(c.getTime())) // NOW

        c.add(Calendar.MONTH, -1)
        Log.d("One month ago ", sdf.format(c.getTime())) // One month ago

        c.add(Calendar.MONTH, -1)
        Log.d("Two month ago ", sdf.format(c.getTime())) // Two month ago
Usama Altaf
  • 90
  • 1
  • 4
  • 23
  • So this i will get list of 3 months so i can pass it into my tab layout which i am using with viewpager 2 – Saif Khan Jun 25 '21 at 10:56
  • Yes you will get JUNE MAY and April – Usama Altaf Jun 25 '21 at 10:57
  • hello i paste this code in kotlin and pass tabs with this see, view_pager.adapter = adapter view_pager.isUserInputEnabled = false TabLayoutMediator(tabLayout, view_pager) { tab, position -> c.toString() its not working but – Saif Khan Jun 25 '21 at 11:06
  • I updated the answer I gave you the solution of what you wanted you can in your Logs if you don't how to pass this in Adapter then accept this answer and ask another question with your code – Usama Altaf Jun 25 '21 at 11:24
0

java.time

Consider using java.time, the modern Java date and time API, for your date work. Sorry that I can write only Java.

    Month thisMonth = Month.from(YearMonth.now(ZoneId.systemDefault()));
    List<Month> months
            = Arrays.asList(thisMonth.minus(2), thisMonth.minus(1), thisMonth);
    for (Month m : months) {
        System.out.println(m.getDisplayName(TextStyle.FULL_STANDALONE, Locale.ENGLISH));
    }

Output when running this month:

April
May
June

And don’t worry: this will work across New Year too. January minus 2 months will be November, for example.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161