-1

I am using SimpleDateFormat to get the current month but i want to show a recyclerview table with information of this month and past three months.

My php json loads this but i want to put automatically it in android.

    periodo1 = findViewById(R.id.tittle_periodo1);
    periodo2 = findViewById(R.id.tittle_periodo2);
    periodo3 = findViewById(R.id.tittle_periodo3);
    periodo4 = findViewById(R.id.tittle_periodo4);

    SimpleDateFormat month_date = new SimpleDateFormat("yyyyMM");
    String month1 = month_date.format(c.getTime());
    periodo1.setText(month1);
www.hybriscx.com
  • 1,129
  • 4
  • 22
G. Romero
  • 47
  • 6
  • 1
    `SimpleDateFormat` is used for date _formatting_, not for navigating dates. See the `Calendar` answer below and work from there. – maksimov Oct 31 '19 at 20:38
  • 3
    @maksimov Nope. One shouldn't use `Calendar`, that class is obsolete. Instead, classes from the `java.time` package should be used. On Android API levels below 26, the ThreeTen AP Backport can be used. – MC Emperor Oct 31 '19 at 21:16
  • Thanks, I know. I was hoping the author would become curious too - considering the comments below. Googling "Android LocalDate" gives you everything you need to know. – maksimov Oct 31 '19 at 23:18
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Calendar`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `YearMonth` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Nov 01 '19 at 05:12

2 Answers2

1

With Java8 syntax you can use time library to achieve this

import java.time.LocalDate;

LocalDate now = LocalDate.now(); // 2019-11-01 (Nov)

LocalDate minusOneMonth = now.minusMonths(1); // 2019-10-01 
minusOneMonth.getMonth().getValue(); // Gives -1 month (10)

LocalDate minusTwoMonth = now.minusMonths(2); // 2019-09-01 
minusTwoMonth.getMonth().getValue(); // Gives -2 month (09)

Hope that's what you are looking for.

If you can't use Java8 syntax, use following

    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.add(Calendar.MONTH, -1);
    Date newDate = cal.getTime();

.... = new SimpleDateFormat("M")
www.hybriscx.com
  • 1,129
  • 4
  • 22
  • Only on Android 8.0 and upwards – maksimov Oct 31 '19 at 20:23
  • I have added 1 more way to my answer after reading your comment. Please check – www.hybriscx.com Oct 31 '19 at 20:31
  • This addition doesn't answer the question in any way. – maksimov Oct 31 '19 at 20:31
  • i tried this but stopped while i was following comments, the solution is good too but i am using latest Xiaomi Redmi Note 7 to dev it and it gave me some warning messages. I apreciate your help! – G. Romero Oct 31 '19 at 21:45
  • @maksimov - Indeed Java 8 is not a syntax but if you really wish to fix English or Grammar, use edit function given by Stackoverflow instead of using a downvote. Another thing, author didn't mention java 5,6,7 or 8. So my answer doesn't become invalid. Last, another person had already added Calendar example, and hence I answere with dateformat initially but later I had to copy it too as I didn't know that people here will downvote quickly or point out language gaps. I didn't know I should double check grammar before posting here. :-) Anyways Thanks and Good luck 4 ur future – – www.hybriscx.com Nov 01 '19 at 01:26
  • For Android versions prior to 8.0 add ThreeTenABP to your project, change the import to `import org.threeten.bp.LocalDate;`, and it works. ThreeTenABP is the Android adaptation of the backport of java.time also known as JSR-310. – Ole V.V. Nov 01 '19 at 17:45
  • What warnings, please? @G.Romero The java.time code is definitely preferred over the code using the old and poorly designed `Calendar` class. – Ole V.V. Nov 01 '19 at 20:01
1

try this :

   Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MONTH, -3); // -3 is Number of months past (july)
    Date newDate = calendar.getTime();

and you can format it if you want :

  String date = DateFormat.format("MM/dd/yyyy", newDate).toString();
mohammadReza Abiri
  • 1,759
  • 1
  • 9
  • 20
  • A `CharSequence`?? – maksimov Oct 31 '19 at 20:26
  • `java.text.DateFormat#format` returns a `String`. There is no possible reason to downcast this object to a `CharSequence` here. Likewise, you shouldn't call `toString()` here, it does nothing. Also note that the author is already using `SimpleDateFormat`, and this isn't what their question is about. – maksimov Oct 31 '19 at 20:35
  • Yes, you are right but I just want to give him the right answer! – mohammadReza Abiri Oct 31 '19 at 20:42
  • 1
    @G.Romero please accept the answer that worked. It will help others with a similar question. – maksimov Oct 31 '19 at 23:04