Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
184 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...