3

I want to get day name of the week from the day number of the year in android. Example: what would be the day name of 344th day of year 2016. My server gives me data like this & have to use this to find out the day name.

{
  "id": 106,
  "month": 12,
  "year": 2016,
  "yDay": 344,
  "hour": 19
}

I can get the current day number from the calendar.

Calendar calendar = Calendar.getInstance(); int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);

Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74

8 Answers8

6

You can get the day of the week as a String directly from the Calendar object:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_YEAR, dayOfYear); // Set the day of the year value
String day = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US); 
// Use Calendar.SHORT for abbreviated names or .LONG for full names
// and be sure to change the locale as necessary
Sammy T
  • 1,924
  • 1
  • 13
  • 20
3

One way how you could achieve it by doing something like this:

 public String getNameOfDay(int year, int dayOfYear) {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
    String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);

    return days[dayIndex - 1];
}                    
Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
2

Firstly, you need to init a Calendar instance with the year and day of year:

Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_YEAR, yourDayOfYear);
c.set(Calendar.YEAR, yourYear);

Then you can get the day of week from Calendar

int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
Pavya
  • 6,015
  • 4
  • 29
  • 42
Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
1

You can get dayOfWeek from date (form date from yDay, month, year)

Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
Jainendra
  • 24,713
  • 30
  • 122
  • 169
0

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

so you will get the current day and same way you will get current date and in th esame way number of days for same date . just calculate now

Community
  • 1
  • 1
Vikas Rana
  • 26
  • 7
0

Try this it gives you the name according to your year and month

// Assuming that you already have this.
    int year = 2016;
    int month = 12;
    int day = 2;

    // First convert to Date. This is one of the many ways.
    String dateString = String.format("%d-%d-%d", year, month, day);
    Date date = new SimpleDateFormat("yyyy-M-d").parse(dateString);

    // Then get the day of week from the Date based on specific locale.
    String dayOfWeek = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date);

    System.out.println(dayOfWeek); // Friday

if you have format of date then you can try this

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dayName("2016-12-02 00:00:00", "YYYY-MM-DD HH:MM:ss")
}

public static String dayName(String inputDate, String format){
    Date date = null;
    try {
       date = new SimpleDateFormat(format).parse(inputDate);
    } catch (ParseException e) {
       e.printStackTrace();
    }
   return new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date);
}
Deepak Sachdeva
  • 950
  • 8
  • 16
0

Another way of accomplishing this would be using the Joda Time Package. Android currently only runs on Java 1.7 and thus does not have good time features by itself.

Add Joda Time to your build.gradle:

compile 'joda-time:joda-time:2.9.4'

and then you can do something similar to this:

public String getNameOfDay(int year, int yDay) {

    String pattern = year + ";" + yDay;

    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("y;D");
    DateTime requestedDate = DateTime.parse(pattern, dateTimeFormatter);
    int dayOfWeek = requestedDate.getDayOfWeek();

    //Notice that Monday is 1 and Sunday is 7
    String[] days = {"Monday", "Tuesday", "Thursday", "Friday", "Saturday", "Sunday"};
    return days[dayOfWeek - 1];
}

Please notice, that I am only offering an alternative to the already posted ways of accomplishing this goal. If you don't want to add joda time to your project you can just go with the ways what the others have already said.

BlueWizard
  • 372
  • 4
  • 19
0

Here is the method that I use, and make sure that you use the same format which means don't use - instead of / or vise versa:

 public String getDayName(String date) {
        SimpleDateFormat df = new SimpleDateFormat("M-d-yyyy", Locale.getDefault());

        Date readDate=null;
        try {
            readDate = df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(Objects.requireNonNull(readDate));

        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

        return days[dayOfWeek-1];
    }
Mohamed Ben Romdhane
  • 1,005
  • 3
  • 11
  • 22