What Is the Easiest Way to Get the Current Day of the Week in Android

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

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);

How can I get all the days of the current week in android?

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.

Getting current week days with dates

This code will work using system first day of week, that might be different from Sunday.

    Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd.MM.yyyy");

for (int i = 0; i < 7; i++) {
Log.i("dateTag", sdf.format(cal.getTime()));
cal.add(Calendar.DAY_OF_WEEK, 1);
}

How to get current time and date in Android

You could use:

import java.util.Calendar;
import java.util.Date;

Date currentTime = Calendar.getInstance().getTime();

There are plenty of constants in Calendar for everything you need.

Check the Calendar class documentation.

How to get date of the first day of the current week in android

You are making it much more complicated than it needs to be.

The code below will give you what you want:

    Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String outputDate = simpleDateFormat.format(calendar.getTime());

Calendar.getInstance() gives a Calendar object set to todays date.

Then i set the DAY_OF_WEEK to MONDAY (note that Calendar.MONDAY's constant value is 2, not 1. This can be seen in the documentation).

I set up a SimpleDateFormat with the correct format, and then i use getTime() on the calendar object to get a Date object which i pass to the SimpleDateFormat.

The difference between the Date class and the Calendar class is that the Calendar class is used to do manipulations on the date (add, subtract, compare, etc), while the Date class is simply used to hold a date, and print it to the screen for the user to see.

So you should always have a Calendar object in the code, and then convert it as i do above, when showing it to the user. The Calendar class has ALOT more functions to work with than the Date class, and it's generally easier.

How to get current day of week like monday etc and compare it in android

Try this:

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

switch(day) {
case Calendar.MONDAY:
imageViewMon.setColorFilter(0xFFFF3D60);
break;
...
}

How the get the current day name using particular date in android?

Use java date formats.

SimpleDateFormat inFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = inFormat.parse(input);
SimpleDateFormat outFormat = new SimpleDateFormat("EEEE");
String goal = outFormat.format(date);


Related Topics



Leave a reply



Submit