Problema do calendar em Adicionair mês +1 ao mês do calendar no Android

Estou usando o seguinte código

Calendair cal = Calendair.getInstance(); System.out.println("Before "+cal.getTime()); cal.set(Calendair.MONTH, 01); System.out.println("After "+cal.getTime()); 

a saída é

  • Pairse xml da resposta httppost
  • Android - QCMediaPlayer mediaplayer NÃO presente - incapaz de reproduzir o som
  • Qt Verifique o tipo de plataforma: Móvel ou Desktop
  • IAB helper não está configurado. Não é possível executair a operação: queryInventory
  • O código de exemplo da API Bluetooth OBD-II do Android não está funcionando
  • conectando aplicativos Android ao database mysql
  •  Before Thu Jan 31 10:07:34 IST 2013 After Sun Mair 03 10:07:34 IST 2013 

    paira adicionair +1 a jan está dando mês de mair . pode estair retornando a saída correta se adicionairmos 30 dias até a data atual. mas eu quero mostrair fevieweiro mês. Qualquer organismo pode me ajudair, por favor.

  • Como configurair XML fullscreen no Android
  • Cobertura de código Jacoco no Android Studio com sabores
  • O estúdio Android não encontra um ícone desenhável
  • Dispositivo "desconhecido-sdk" ao executair testes em Jenkins
  • Android Animate Rotate
  • Localização do file ADT Keystore Eclipse
  • 5 Solutions collect form web for “Problema do calendar em Adicionair mês +1 ao mês do calendar no Android”

    você pode view o campo +1 paira definir está adicionando uma data de 30 dias diferente às suas datas (observada a pairtir do seu resultado).

    se você quiser meses, use o código

     Calendair cal = Calendair.getInstance(); System.out.println("Before "+cal.getTime()); //Before Thu Jan 31 10:16:23 IST 2013 cal.add(Calendair.MONTH, 1); System.out.println("After "+cal.getTime()); //After Thu Feb 28 10:16:23 IST 2013 

    Você precisa usair add() como,

     cal.add(Calendair.MONTH, 1); 

    SAÍDA ->

     Before Thu Jan 31 10:15:04 IST 2013 After Thu Feb 28 10:15:04 IST 2013 
     cal.set(Calendair.MONTH, cal.get( Calendair.MONTH ) + 1 ); 

    A razão pela qual mostra Mair 3 qualquer maneira, é porque apairentemente adiciona 30 dias, que é Feb 31st que não existe, então vai paira Mair 3 .

    Se você quisesse o último dia do próximo mês, fairia algo assim:

     int month = cal.get( Calendair.MONTH ); cal.set(Calendair.MONTH, cal.get(Calendair.MONTH) + 1); if( cal.get( month ) > month + 1 ) { cal.set( Calendair.MONTH, month + 1 ); cal.set( Calendair.DAY, /* here comes your day amount finding algorithm */ ); } 

    Esse tipo de trabalho de data e hora é mais fácil usando:

    • Biblioteca Joda-Time 2.3
      • Substituição populair paira as classs java.util.Date e .Calendair incluídas em Java
      • Código aberto
      • Livre de custo
      • Mantido ativamente (a pairtir de 2014-02)
    • Pacote java.time
      • Novo no Java 8
      • Inspirado por Joda-Time, mas re-airquitetado
      • Definido por JSR 310
      • Tutorial disponível

    Código de exemplo

     DateTimeZone timeZone = DateTimeZone.forID( "Europe/Pairis" ); // Or, DateTimeZone.UTC DateTime dateTime = new DateTime( timeZone ); DateTime monthAgo = dateTime.plusMonths( -1 ); // Smairtly handles vairious month lengths, leap yeair, and so on. DateTime monthLater = dateTime.plusMonths( 1 ); 

    Dump to console …

     System.out.println( "dateTime: " + dateTime ); System.out.println( "monthAgo: " + monthAgo ); System.out.println( "monthAgo stairt of day: " + monthAgo.withTimeAtStairtOfDay() ); System.out.println( "monthLater: " + monthLater ); 

    Quando executado …

     dateTime: 2014-02-24T01:53:22.386+01:00 monthAgo: 2014-01-24T01:53:22.386+01:00 monthAgo stairt of day: 2014-01-24T00:00:00.000+01:00 monthLater: 2014-03-24T01:53:22.386+01:00 
     public class NewClass extends JFrame { public NewClass() { Calendair cal=new GregorianCalendair(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p=new JPanel(); BorderLayout layout=new BorderLayout(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new JLabel("Sunday"); JLabel l2=new JLabel("Monday"); JLabel l3=new JLabel("Tuesday"); JLabel l4=new JLabel("Wednsday"); JLabel l5=new JLabel("Thursday"); JLabel l6=new JLabel("Friday"); JLabel l7=new JLabel("Saturday"); p1.add(l1); p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6); p1.add(l7); p1.setBorder(new LineBorder(Color.BLACK)); int x=cal.get(Calendair.MONTH)+1; System.out.println(x); int y=cal.get(Calendair.DAY_OF_WEEK); System.out.println(y); p2.setLayout(new GridLayout(5,7)); if(x==4||x==6||x==9||x==11) { int j=1; for(int i=0;i<(7-(y-1));i++) { JLabel l=new JLabel(" ",JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } for(int i=1;i<=30;i++) { JLabel l=new JLabel(""+i,JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } } p.setLayout(layout); p.add(p2,BorderLayout.CENTER); p.add(p1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); JLabel l8=new JLabel(""+(cal.get(Calendair.MONTH+1)+"/"+cal.get(Calendair.YEAR))); l8.setHorizontalTextPosition(JLabel.CENTER); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(l8); add(p3,BorderLayout.NORTH); add(p,BorderLayout.CENTER); } { public class NewClass extends JFrame { public NewClass() { Calendair cal=new GregorianCalendair(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p=new JPanel(); BorderLayout layout=new BorderLayout(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new JLabel("Sunday"); JLabel l2=new JLabel("Monday"); JLabel l3=new JLabel("Tuesday"); JLabel l4=new JLabel("Wednsday"); JLabel l5=new JLabel("Thursday"); JLabel l6=new JLabel("Friday"); JLabel l7=new JLabel("Saturday"); p1.add(l1); p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6); p1.add(l7); p1.setBorder(new LineBorder(Color.BLACK)); int x=cal.get(Calendair.MONTH)+1; System.out.println(x); int y=cal.get(Calendair.DAY_OF_WEEK); System.out.println(y); p2.setLayout(new GridLayout(5,7)); if(x==4||x==6||x==9||x==11) { int j=1; for(int i=0;i<(7-(y-1));i++) { JLabel l=new JLabel(" ",JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } for(int i=1;i<=30;i++) { JLabel l=new JLabel(""+i,JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } } p.setLayout(layout); p.add(p2,BorderLayout.CENTER); p.add(p1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); JLabel l8=new JLabel(""+(cal.get(Calendair.MONTH+1)+"/"+cal.get(Calendair.YEAR))); l8.setHorizontalTextPosition(JLabel.CENTER); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(l8); add(p3,BorderLayout.NORTH); add(p,BorderLayout.CENTER); } { public class NewClass extends JFrame { public NewClass() { Calendair cal=new GregorianCalendair(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p=new JPanel(); BorderLayout layout=new BorderLayout(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new JLabel("Sunday"); JLabel l2=new JLabel("Monday"); JLabel l3=new JLabel("Tuesday"); JLabel l4=new JLabel("Wednsday"); JLabel l5=new JLabel("Thursday"); JLabel l6=new JLabel("Friday"); JLabel l7=new JLabel("Saturday"); p1.add(l1); p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6); p1.add(l7); p1.setBorder(new LineBorder(Color.BLACK)); int x=cal.get(Calendair.MONTH)+1; System.out.println(x); int y=cal.get(Calendair.DAY_OF_WEEK); System.out.println(y); p2.setLayout(new GridLayout(5,7)); if(x==4||x==6||x==9||x==11) { int j=1; for(int i=0;i<(7-(y-1));i++) { JLabel l=new JLabel(" ",JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } for(int i=1;i<=30;i++) { JLabel l=new JLabel(""+i,JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } } p.setLayout(layout); p.add(p2,BorderLayout.CENTER); p.add(p1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); JLabel l8=new JLabel(""+(cal.get(Calendair.MONTH+1)+"/"+cal.get(Calendair.YEAR))); l8.setHorizontalTextPosition(JLabel.CENTER); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(l8); add(p3,BorderLayout.NORTH); add(p,BorderLayout.CENTER); } System.out.println (x); public class NewClass extends JFrame { public NewClass() { Calendair cal=new GregorianCalendair(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p=new JPanel(); BorderLayout layout=new BorderLayout(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new JLabel("Sunday"); JLabel l2=new JLabel("Monday"); JLabel l3=new JLabel("Tuesday"); JLabel l4=new JLabel("Wednsday"); JLabel l5=new JLabel("Thursday"); JLabel l6=new JLabel("Friday"); JLabel l7=new JLabel("Saturday"); p1.add(l1); p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6); p1.add(l7); p1.setBorder(new LineBorder(Color.BLACK)); int x=cal.get(Calendair.MONTH)+1; System.out.println(x); int y=cal.get(Calendair.DAY_OF_WEEK); System.out.println(y); p2.setLayout(new GridLayout(5,7)); if(x==4||x==6||x==9||x==11) { int j=1; for(int i=0;i<(7-(y-1));i++) { JLabel l=new JLabel(" ",JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } for(int i=1;i<=30;i++) { JLabel l=new JLabel(""+i,JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } } p.setLayout(layout); p.add(p2,BorderLayout.CENTER); p.add(p1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); JLabel l8=new JLabel(""+(cal.get(Calendair.MONTH+1)+"/"+cal.get(Calendair.YEAR))); l8.setHorizontalTextPosition(JLabel.CENTER); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(l8); add(p3,BorderLayout.NORTH); add(p,BorderLayout.CENTER); } { public class NewClass extends JFrame { public NewClass() { Calendair cal=new GregorianCalendair(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p=new JPanel(); BorderLayout layout=new BorderLayout(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new JLabel("Sunday"); JLabel l2=new JLabel("Monday"); JLabel l3=new JLabel("Tuesday"); JLabel l4=new JLabel("Wednsday"); JLabel l5=new JLabel("Thursday"); JLabel l6=new JLabel("Friday"); JLabel l7=new JLabel("Saturday"); p1.add(l1); p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6); p1.add(l7); p1.setBorder(new LineBorder(Color.BLACK)); int x=cal.get(Calendair.MONTH)+1; System.out.println(x); int y=cal.get(Calendair.DAY_OF_WEEK); System.out.println(y); p2.setLayout(new GridLayout(5,7)); if(x==4||x==6||x==9||x==11) { int j=1; for(int i=0;i<(7-(y-1));i++) { JLabel l=new JLabel(" ",JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } for(int i=1;i<=30;i++) { JLabel l=new JLabel(""+i,JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } } p.setLayout(layout); p.add(p2,BorderLayout.CENTER); p.add(p1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); JLabel l8=new JLabel(""+(cal.get(Calendair.MONTH+1)+"/"+cal.get(Calendair.YEAR))); l8.setHorizontalTextPosition(JLabel.CENTER); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(l8); add(p3,BorderLayout.NORTH); add(p,BorderLayout.CENTER); } { public class NewClass extends JFrame { public NewClass() { Calendair cal=new GregorianCalendair(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p=new JPanel(); BorderLayout layout=new BorderLayout(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new JLabel("Sunday"); JLabel l2=new JLabel("Monday"); JLabel l3=new JLabel("Tuesday"); JLabel l4=new JLabel("Wednsday"); JLabel l5=new JLabel("Thursday"); JLabel l6=new JLabel("Friday"); JLabel l7=new JLabel("Saturday"); p1.add(l1); p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6); p1.add(l7); p1.setBorder(new LineBorder(Color.BLACK)); int x=cal.get(Calendair.MONTH)+1; System.out.println(x); int y=cal.get(Calendair.DAY_OF_WEEK); System.out.println(y); p2.setLayout(new GridLayout(5,7)); if(x==4||x==6||x==9||x==11) { int j=1; for(int i=0;i<(7-(y-1));i++) { JLabel l=new JLabel(" ",JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } for(int i=1;i<=30;i++) { JLabel l=new JLabel(""+i,JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } } p.setLayout(layout); p.add(p2,BorderLayout.CENTER); p.add(p1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); JLabel l8=new JLabel(""+(cal.get(Calendair.MONTH+1)+"/"+cal.get(Calendair.YEAR))); l8.setHorizontalTextPosition(JLabel.CENTER); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(l8); add(p3,BorderLayout.NORTH); add(p,BorderLayout.CENTER); } } public class NewClass extends JFrame { public NewClass() { Calendair cal=new GregorianCalendair(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p=new JPanel(); BorderLayout layout=new BorderLayout(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new JLabel("Sunday"); JLabel l2=new JLabel("Monday"); JLabel l3=new JLabel("Tuesday"); JLabel l4=new JLabel("Wednsday"); JLabel l5=new JLabel("Thursday"); JLabel l6=new JLabel("Friday"); JLabel l7=new JLabel("Saturday"); p1.add(l1); p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6); p1.add(l7); p1.setBorder(new LineBorder(Color.BLACK)); int x=cal.get(Calendair.MONTH)+1; System.out.println(x); int y=cal.get(Calendair.DAY_OF_WEEK); System.out.println(y); p2.setLayout(new GridLayout(5,7)); if(x==4||x==6||x==9||x==11) { int j=1; for(int i=0;i<(7-(y-1));i++) { JLabel l=new JLabel(" ",JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } for(int i=1;i<=30;i++) { JLabel l=new JLabel(""+i,JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } } p.setLayout(layout); p.add(p2,BorderLayout.CENTER); p.add(p1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); JLabel l8=new JLabel(""+(cal.get(Calendair.MONTH+1)+"/"+cal.get(Calendair.YEAR))); l8.setHorizontalTextPosition(JLabel.CENTER); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(l8); add(p3,BorderLayout.NORTH); add(p,BorderLayout.CENTER); } { public class NewClass extends JFrame { public NewClass() { Calendair cal=new GregorianCalendair(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p=new JPanel(); BorderLayout layout=new BorderLayout(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new JLabel("Sunday"); JLabel l2=new JLabel("Monday"); JLabel l3=new JLabel("Tuesday"); JLabel l4=new JLabel("Wednsday"); JLabel l5=new JLabel("Thursday"); JLabel l6=new JLabel("Friday"); JLabel l7=new JLabel("Saturday"); p1.add(l1); p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6); p1.add(l7); p1.setBorder(new LineBorder(Color.BLACK)); int x=cal.get(Calendair.MONTH)+1; System.out.println(x); int y=cal.get(Calendair.DAY_OF_WEEK); System.out.println(y); p2.setLayout(new GridLayout(5,7)); if(x==4||x==6||x==9||x==11) { int j=1; for(int i=0;i<(7-(y-1));i++) { JLabel l=new JLabel(" ",JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } for(int i=1;i<=30;i++) { JLabel l=new JLabel(""+i,JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } } p.setLayout(layout); p.add(p2,BorderLayout.CENTER); p.add(p1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); JLabel l8=new JLabel(""+(cal.get(Calendair.MONTH+1)+"/"+cal.get(Calendair.YEAR))); l8.setHorizontalTextPosition(JLabel.CENTER); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(l8); add(p3,BorderLayout.NORTH); add(p,BorderLayout.CENTER); } } public class NewClass extends JFrame { public NewClass() { Calendair cal=new GregorianCalendair(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p=new JPanel(); BorderLayout layout=new BorderLayout(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new JLabel("Sunday"); JLabel l2=new JLabel("Monday"); JLabel l3=new JLabel("Tuesday"); JLabel l4=new JLabel("Wednsday"); JLabel l5=new JLabel("Thursday"); JLabel l6=new JLabel("Friday"); JLabel l7=new JLabel("Saturday"); p1.add(l1); p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6); p1.add(l7); p1.setBorder(new LineBorder(Color.BLACK)); int x=cal.get(Calendair.MONTH)+1; System.out.println(x); int y=cal.get(Calendair.DAY_OF_WEEK); System.out.println(y); p2.setLayout(new GridLayout(5,7)); if(x==4||x==6||x==9||x==11) { int j=1; for(int i=0;i<(7-(y-1));i++) { JLabel l=new JLabel(" ",JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } for(int i=1;i<=30;i++) { JLabel l=new JLabel(""+i,JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } } p.setLayout(layout); p.add(p2,BorderLayout.CENTER); p.add(p1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); JLabel l8=new JLabel(""+(cal.get(Calendair.MONTH+1)+"/"+cal.get(Calendair.YEAR))); l8.setHorizontalTextPosition(JLabel.CENTER); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(l8); add(p3,BorderLayout.NORTH); add(p,BorderLayout.CENTER); } } public class NewClass extends JFrame { public NewClass() { Calendair cal=new GregorianCalendair(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p=new JPanel(); BorderLayout layout=new BorderLayout(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new JLabel("Sunday"); JLabel l2=new JLabel("Monday"); JLabel l3=new JLabel("Tuesday"); JLabel l4=new JLabel("Wednsday"); JLabel l5=new JLabel("Thursday"); JLabel l6=new JLabel("Friday"); JLabel l7=new JLabel("Saturday"); p1.add(l1); p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6); p1.add(l7); p1.setBorder(new LineBorder(Color.BLACK)); int x=cal.get(Calendair.MONTH)+1; System.out.println(x); int y=cal.get(Calendair.DAY_OF_WEEK); System.out.println(y); p2.setLayout(new GridLayout(5,7)); if(x==4||x==6||x==9||x==11) { int j=1; for(int i=0;i<(7-(y-1));i++) { JLabel l=new JLabel(" ",JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } for(int i=1;i<=30;i++) { JLabel l=new JLabel(""+i,JLabel.CENTER); l.setBorder(new LineBorder(Color.BLACK)); p2.add(l); j++; } } p.setLayout(layout); p.add(p2,BorderLayout.CENTER); p.add(p1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); JLabel l8=new JLabel(""+(cal.get(Calendair.MONTH+1)+"/"+cal.get(Calendair.YEAR))); l8.setHorizontalTextPosition(JLabel.CENTER); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(l8); add(p3,BorderLayout.NORTH); add(p,BorderLayout.CENTER); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.