I want to get all the days in the current week in an array of Strings. How can I acheive this? Thanks in advance.
I think you want something like this... assuming you always want weeks starting on Monday, and a date format of "MM/dd/yyyy".
DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String[] days = new String[7];
for (int i = 0; i < 7; i++)
{
days[i] = format.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
While I think that's correct, I personally use Joda Time wherever possible - I'm aware that's slightly trickier on Android where you're more likely to have space concerns.
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("EEEE");
formattedDate = df.format(c.getTime());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With