miércoles, 26 de enero de 2011

[Java] 7 days in a string array


Here you have a code snippet to have an String array
with the last 7 dates or the las week, it is wrriten
in java, it can be usefull somehow. The format is yyyy-MM-dd

I hope you find this usefull.


public static String[] SevenDaysFromNow(){
String[] days = new String[7];
Date today = new Date();
long times = today.getTime();
long oneday = (24 * 60 * 60 * 1000);
for(int i=0; i<7; i++){
times -= oneday;
Date tmp = new Date(times);
String DATE_FORMAT_NOW = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
String MyDate = sdf.format(tmp);
days[i] = MyDate;
}
return days;
}


Also you want to import the following to make it work.

import java.util.Date;
import java.text.SimpleDateFormat;

No hay comentarios:

Publicar un comentario