In android,I have a String of that format "2015-03-30T12:30:00"
and I want to know which day of the week it is.
Testing with the device on the same day, with that function dayOfweek = 5
and dayOfWeek2 = 2
why?
if I try to create a new Date with year , month , day Its say is deprecated...
public int devolverDia(String hora)
{
hora = hora.substring(0, 10);
String weekdays[] = new DateFormatSymbols(Locale.ENGLISH).getWeekdays();
Calendar c = Calendar.getInstance();
int year = Integer.parseInt(hora.substring(0, 4));
int month = Integer.parseInt(hora.substring(5, 7));
int day = Integer.parseInt(hora.substring(8, 10));
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
c.setTime(new Date());
int dayOfWeek2 = c.get(Calendar.DAY_OF_WEEK);
return dayOfWeek;
}
The reason that you don't get the date that you expect is that the line
c.set(Calendar.MONTH, month);
expects a month number from 0 (for January) to 11 (for December). If you replace it with
c.set(Calendar.MONTH, month - 1);
this code will work.
However, a much better solution would be to use the parse
method of the SimpleDateFormat
class to convert your String
to a Date
.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
c.setTime(format.parse(hora));