I’d like to parse given date by user but with a specific format : the day name of the week.
So far, here’s what I did :
SimpleDateFormat format = new SimpleDateFormat("E", Locale.ENGLISH);
try {
Date date = format.parse ("Friday");
Calendar calendar = format.getCalendar();
calendar.add(Calendar.WEEK_OF_YEAR, 1);
return calendar.getTime();
}
catch (ParseException pe) {
return null;
}
But this returns me :
2 janv. 1970 00:00:00
I’d like it to return me the future friday (in our timelapse, 4 nov. 2011)
How can I do ?
Parse just the day, put that into a
Calendarso that you can find the day of the week, then create another calendar based on the current date so that you can set the day of week:Note that that won’t necessarily put it into the future – it’ll set the day within the current week. So you may want to add a week afterwards, if you want to ensure that it’s really in the future:
(You should check what behaviour you want if the current day of week is the same as the parsed one…)
As an aside, I’d personally recommend using Joda Time for all serious date and time work in Java. It’s a much nicer API.