JhonF
JhonF

Reputation: 199

Want to get day of a week as a string, But giving wrong day

I tried to get the day as a string by using the following code. But it returns wrong string. Can I fix it with this code.

private String getDayOfWeek(int value){
    String day = "";
    switch(value){
    case 1:
        day="Sunday";
        break;
    case 2:
        day="Monday";
        break;
    case 3:
        day="Tuesday";
        break;
    case 4:
        day="Wednesday";
        break;
    case 5:
        day="Thursday";
        break;
    case 6:
        day="Friday";
        break;
    case 7:
        day="Saturday";
        break;
    }
    return day;

I implements it as

Calendar c = Calendar.getInstance();    
String dayOfWeek = getDayOfWeek(Calendar.DAY_OF_WEEK);
System.out.println(dayOfWeek);

Upvotes: 9

Views: 50026

Answers (8)

Kuchi
Kuchi

Reputation: 4674

DayOfWeek::getDisplayName

There is a nice way of doing this with the DateTime API since Java 8

LocalDate localDate = LocalDate.parse("2018-08-29");
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
String displayName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.ENGLISH);

Now displayName equals Wednesday.

With DayOfWeek.of(int dayOfWeek) there is no need for a switch statement.

By number

To access the DayOfWeek by number as seen in the Question, use 1-7 for Monday-Sunday, per the ISO 8601 standard.

DayOfWeek.of( 1 ).getDisplayName(TextStyle.FULL, Locale.ENGLISH)

Monday

By constant

Or use one of the seven named enum objects.

DayOfWeek.MONDAY.getDisplayName(TextStyle.FULL, Locale.ENGLISH)

Monday

Upvotes: 9

madhairsilence
madhairsilence

Reputation: 3870

Another brute force way is

List<String> days = Arrays.asList( new String[] {"sunday","monday", "tuesday", "wednesday","thursday","friday","saturday"});

And you can use it as days.get( <your input value> );

Upvotes: 0

Brother
Brother

Reputation: 2210

I think that the best way to get the day of week is with this simple snippet of code:

Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEEEE");
String dayOfWeek = dateFormat.format(date);
System.out.println(dayOfWeek);

If you want to use Calendar:

Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEEEE");
String dayOfWeek = dateFormat.format(c.getTime());
System.out.println(dayOfWeek);

The format "EEEEE" return the day of week: Sunday, Monday .. in the language of the machine, that is better that fix in English on the code.

[UPDATED]

With Java 8+ and introducing java.time package, you can achieve this with:

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.format.TextStyle;
import java.util.Locale;

class Example {
  public static void main(String[] args) {
    final DayOfWeek dayOfWeek = LocalDateTime.now().getDayOfWeek();
    System.out.println(dayOfWeek.getDisplayName(TextStyle.FULL, Locale.US));
  }
}

In your desired Locale, also selecting TextStyle:

  • FULL: Friday
  • SHORT: Fri
  • NARROW: F

Upvotes: 12

Hardik Patel
Hardik Patel

Reputation: 1138

Below is the two line of snippet using Java 1.8 Time API.

LocalDate localDate = LocalDate.of(Integer.valueOf(year),Integer.valueOf(month),Integer.valueOf(day));
String dayOfWeek = String.valueOf(localDate.getDayOfWeek());

Upvotes: 3

Hardik Patel
Hardik Patel

Reputation: 1138

You can use below method to get Day of the Week by passing a specific date,

Here for the set method of Calendar class, Tricky part is the index for the month parameter will starts from 0.

public static String getDay(int day, int month, int year) {

        Calendar cal = Calendar.getInstance();

        if(month==1){
            cal.set(year,0,day);
        }else{
            cal.set(year,month-1,day);
        }

        int dow = cal.get(Calendar.DAY_OF_WEEK);

        switch (dow) {
        case 1:
            return "SUNDAY";
        case 2:
            return "MONDAY";
        case 3:
            return "TUESDAY";
        case 4:
            return "WEDNESDAY";
        case 5:
            return "THURSDAY";
        case 6:
            return "FRIDAY";
        case 7:
            return "SATURDAY";
        default:
            System.out.println("GO To Hell....");
        }

        return null;
    }

Upvotes: 3

vijaya kumar
vijaya kumar

Reputation: 834

use getdisplayname method from calender object.

Calendar currentDate=new GregorianCalendar();

String dayOfWeek = currentDate.getDisplayName( Calendar.DAY_OF_WEEK ,Calendar.LONG, Locale.getDefault());

Upvotes: 15

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

You need to use

String dayOfWeek = getDayOfWeek(c.get(Calendar.DAY_OF_WEEK));

What you were doing before

String dayOfWeek = getDayOfWeek(Calendar.DAY_OF_WEEK);

is calling your method with a random constant (that happens to be 7) the Calendar class is using to represent the DAY_OF_WEEK field in a date.

What you are actually looking for is getting the value of the day of the week in your Calendar instance, which is what Calendar#get(int)

c.get(Calendar.DAY_OF_WEEK)

returns.


On a related note, try to learn and use an actual debugger as stated in the comments.

Upvotes: 17

Marcelo
Marcelo

Reputation: 11308

You can use a SimpleDateFormat for this:

SimpleDateFormat formatter = new SimpleDateFormat("EEEE");
System.out.println(formatter.format(new Date());

This will return the String representation of the current day of the week.

Upvotes: 4

Related Questions