Android: How to Get the Current Day of the Week (Monday, etc...) in the User's Language

Android: how to get the current day of the week (Monday, etc...) in the user's language?

Use SimpleDateFormat to format dates and times into a human-readable string, with respect to the users locale.

Small example to get the current day of the week (e.g. "Monday"):

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date();
String dayOfTheWeek = sdf.format(d);

What is the easiest way to get the current day of the week in Android?

The Java Calendar class works.

Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);

switch (day) {
case Calendar.SUNDAY:
// Current day is Sunday
break;
case Calendar.MONDAY:
// Current day is Monday
break;
case Calendar.TUESDAY:
// etc.
break;
}

For much better datetime handling consider using the Java 8 time API:

String day = LocalDate.now().getDayOfWeek().name()

To use this below Android SDK 26 you'll need to enable Java 8 desugaring in build.gradle:

android {
defaultConfig {
// Required when setting minSdkVersion to 20 or lower
multiDexEnabled true
}

compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
// Sets Java compatibility to Java 8
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}

More information on Android's Java 8 support: https://developer.android.com/studio/write/java8-support

Get short days of week in android

To format a specific date (not necessarily the current date), use EEE in a SimpleDateFormat. If you want all the short days of the week, you could use

DateFormatSymbols symbols = new DateFormatSymbols(locale);
String[] shortDays = symbols.getShortWeekdays();

How to get localization of day in week

As per other questions, you don't need SimpleDateFormat to get the numeric day of the week - that is provided by Calendar directly via the DAY_OF_WEEK field (which goes from 1 to 7 where 1 is SUNDAY and 7 is SATURDAY):

Calendar today = Calendar.getInstance();
int dayOfWeek = today.get(Calendar.DAY_OF_WEEK);
// Day of Week is a number between 1 and 7 where 1 is Sunday.
int dayOfWeekMondayFirst = (dayOfWeek + 5) % 7 + 1;

How to get day of the week from CalendarView

For example:

calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

Calendar calendar = Calendar.getInstance();
calendar.set(year, month, dayOfMonth);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
}
});

How to get the day like Monday string from 2015-02-24 in android


String input_date_string="2015-02-24";
SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
date = dateformat.parse(input_date_string);
DateFormat dayFormate=new SimpleDateFormat("EEEE");
String dayFromDate=dayFormate.format(date);
Log.d("asd", "----------:: "+dayFromDate);

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

How to get first day of the week from given parameters and print the whole week from monday to sunday?

mktime() uses many of the members in struct tm, so calling mktime() without setting those members results in undefined behavior (UB).

// struct tm time;
struct tm time = { 0 }; // set all fields to 0.
...
time.tm_year = year - 1900;
time.tm_mon = month - 1;
time.tm_mday = day;
time_t rightTime = mktime(&time);

After mktime(), the day-of-the-week member is set.

int days_since_Sunday = time.tm_wday; // 0 - 6

... how to find out the first day of that given week, refresh all the information and then print it out for user. ... print the whole week to user from Monday till Sunday.

To find the days since Monday, subtract 1, but use modulo math and avoid negative numbers. % matches the classic functionality of modulo with positive arguments, but differs with negative ones.

#define DaysPerWeek 7
int days_since_Monday = (days_since_Sunday + DaysPerWeek - 1) % DaysPerWeek;

To print the week, start with the given date and subtract the offset:

// Move date back to Monday
time.tm_mday -= days_since_Monday;

for (int d=0; d<DaysPerWeek; d++) {
mktime(&time); // adjust for end of month, year issues.
strftime(buffer, sizeof buffer, "%A %d. %B %Y\n", &time);
printf("%s", buffer);
time.tm_mday++; // advance to tomorrow
}

Note: using time as in struct tm time is OK, but is a bit confusing given the standard function time(). Consider using a different name. Perhaps struct tm timestamp_ymd.

Robust code would add error checking:

if (mktime(&time) == (time_t)-1) Handle_Error();

i want to show days of week from sunday to monday from the string [1,1,1,1,1,1,1] in android

UPDATED

 List<String> weekDayslist = new ArrayList<String>();
String availabilityDays = "";
String frequency;

frequency = getIntent().getStringExtra("Frequency");
if (frequency != null) {
availabilityDays = getIntent().getStringExtra("DaysOfDelivery");
}

if (!availabilityDays.isEmpty()) {
availabilityDays = availabilityDays.replaceAll("[\(\)\[\]\\{\\}]", "");
for (String field : availabilityDays.split(",")
)
weekDayslist.add(field);
}

Get value of day month from Date object in Android?




import android.text.format.DateFormat;

String dayOfTheWeek = (String) DateFormat.format("EEEE", date); // Thursday
String day = (String) DateFormat.format("dd", date); // 20
String monthString = (String) DateFormat.format("MMM", date); // Jun
String monthNumber = (String) DateFormat.format("MM", date); // 06
String year = (String) DateFormat.format("yyyy", date); // 2013


Related Topics



Leave a reply



Submit