Boa tarde,
tenho um metodo que tem que me retornar o mes anterior apartir do mes atual;
mas o o Date esta dando deprecated…
como eu faria o codigo abaixo, usando Calendar?
tentei mas nao to conceguindo!
SimpleDateFormat f = new SimpleDateFormat("MMMyy");
Date d = new Date();
int mes = d.getMonth();
d.setMonth(mes-1);
int ano = d.getYear();
if(mes==0){
d.setMonth(11);
d.setYear(ano-1);
}
String tot = f.format(d);
System.out.println(">>>"+tot);
Obrigado desde já!
Vê aew:
[code]
SimpleDateFormat f = new SimpleDateFormat(“MMMyy”);
Calendar calendar = Calendar.getInstance();
int mes = calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, mes - 1);
int ano = calendar.get(Calendar.YEAR);
if (mes == 0) {
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.YEAR, ano - 1);
}
String tot = f.format(calendar.getTime());
System.out.println(">>>" + tot);[/code]
Acho que é isso. Vê se resolve! Flw! :mrgreen:
pow brother brigadao!
eu nao sei usar o Calendar.
vou estudar apartir do seu cod…
mt obrigado !!!
Fala ai blz!
Coloca esses sites aqui no teu favoritos:
Javadoc (originally cased JavaDoc) is a documentation generator created by Sun Microsystems for the Java language (now owned by Oracle Corporation) for generating API documentation in HTML format from Java source code. The HTML format is used for adding the convenience of being able to hyperlink related documents together.
The "doc comments" format used by Javadoc is the de facto industry standard for documenting Java classes. Some IDEs, like IntelliJ IDEA, NetBeans and Eclipse, automatically ge...
http://java.sun.com/javase/6/docs/api/
Procura o Calendar neles.
Dá uma olhada em GregorianCalendar!
Abraços!
Para manipular datas, use o JODA time!
http://joda-time.sourceforge.net/
Além de fácil de usar e bem documentada, a API facilita em 6 milhões de vezes manipulação de datas e tempo.
O seu exemplo mesmo ficaria assim:
DateMidnight d = new DateMidnight();
d.minusMonths(1);
System.out.println(d.toString("MMMyy"));
O JODA também tem um suporte muito melhor ao horário de verão e aos fusos.
Confira!
pbnf
Agosto 30, 2007, 11:37am
#6
Na seção de Artigos e Tutoriais aqui do GUJ tem um artigo falando como trabalhar com datas, e é bem mais fácil do que o codigo aqui postado!
cbridi
Setembro 4, 2007, 11:27am
#8
Outra forma, sem ifs…
SimpleDateFormat f = new SimpleDateFormat("MMMyy");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
String tot = f.format(calendar.getTime());
System.out.println(">>>" + tot);