-1

Im trying to get the number of the month (from 1 to 12) of the year and the year. But when i use that code it give me only the number 2 for the month and the number 1 for the year.

 Calendar calendar=Calendar.getInstance();
    String currentdate;
    SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/YYYY");
    currentdate=sdf.format(calendar.getTime());

    Calendario.gprec=calendar.DAY_OF_MONTH;
    Calendario.mprec=calendar.MONTH;
    Calendario.aprec=calendar.YEAR;
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Kyo R.C.
  • 13
  • 6
  • 1
    Possible duplicate of [Java Calendar always shows the same time](https://stackoverflow.com/questions/26082851/java-calendar-always-shows-the-same-time) – Ole V.V. Aug 25 '18 at 07:10
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and its also outdated friend `Calendar`, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Aug 25 '18 at 07:13
  • Also check the case of your format pattern letters. Uppercase `YYYY` will give you unpleasant surprises. – Ole V.V. Aug 25 '18 at 07:14

1 Answers1

1

You are assigning static value of field . Whereas you have to get the value at field from Calender . So your code should look like as:

  Calendar calendar=Calendar.getInstance();
  Calendario.gprec=calendar.get(Calendar.DAY_OF_MONTH);
  Calendario.mprec=calendar.get(Calendar.MONTH);
  Calendario.aprec=calendar.get(Calendar.YEAR);
ADM
  • 20,406
  • 11
  • 52
  • 83