java - Android Calendar get current day of week as string -
this question has answer here:
i tried string name of actual weekday way:
calendar c = calendar.getinstance(); int dayofweek = c.get(calendar.day_of_week); if (c.get(calendar.monday) == dayofweek) weekday = "monday"; else if (c.get(calendar.tuesday) == dayofweek) weekday = "tuesday"; else if (c.get(calendar.wednesday) == dayofweek) weekday = "wednesday"; else if (c.get(calendar.thursday) == dayofweek) weekday = "thursday"; else if (c.get(calendar.friday) == dayofweek) weekday = "friday"; else if (c.get(calendar.saturday) == dayofweek) weekday = "saturday"; else if (c.get(calendar.sunday) == dayofweek) weekday = "sunday";
but weekday
stays null , have no idea why because debugger says dayofweek
5 should equal c.get(calendar.thursday)
i accomplished doing following:
string weekday; simpledateformat dayformat = new simpledateformat("eeee", locale.us); calendar calendar = calendar.getinstance(); weekday = dayformat.format(calendar.gettime());
- use day format "eeee" return full weekday name, i.e. tuesday
- use day format "e" return the abbreviated name, i.e. tue
- another benefit returning format in android translate weekday based on locale.
you want review simpledateformat learn more. think cleanest approach in not need switch or if statement if need string value.
Comments
Post a Comment