7

I want to find out the day of the date in Java for a date like 27-04-2011.

I tried to use this, but it doesn't work:

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

It gives the integer value, not the String output I want. I am not getting the correct value I want. For example it is giving me value 4 for the date 28-02-2011 where it should be 2 because Sunday is the first week day.

jmj
  • 237,923
  • 42
  • 401
  • 438
riyana
  • 21,385
  • 10
  • 35
  • 34

9 Answers9

9

Yes, you've asked it for the day of the week - and February 28th was a Monday, day 2. Note that in the code you've given, you're not actually setting the date anywhere - it's just using the current date, which is a Wednesday, which is why you're getting 4. If you could show how you're trying to set the calendar to a different date (e.g. 28th of February) we can work out why that's not working for you.

If you want it formatted as text, you can use SimpleDateFormat and the "E" specifier. For example (untested):

SimpleDateFormat formatter = new SimpleDateFormat("EEE");
String text = formatter.format(cal.getTime());

Personally I would avoid using Calendar altogether though - use Joda Time, which is a far superior date and time API.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3
Calendar cal=Calendar.getInstance();
System.out.println(new SimpleDateFormat("EEE").format(cal.getTime()));

Output

Wed

See Also

jmj
  • 237,923
  • 42
  • 401
  • 438
3
  String dayNames[] = new DateFormatSymbols().getWeekdays();
  Calendar date1 = Calendar.getInstance();
  System.out.println("Today is a " 
    + dayNames[date1.get(Calendar.DAY_OF_WEEK)]);
Ammu
  • 5,067
  • 9
  • 34
  • 34
2

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice at the Home Page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String input = "27-04-2011";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-M-u", Locale.ENGLISH);
        LocalDate date = LocalDate.parse(input, dtf);
        DayOfWeek dow = date.getDayOfWeek();
        System.out.println(dow);

        // String value
        String strDay = dow.getDisplayName(TextStyle.FULL, Locale.ENGLISH);
        System.out.println(strDay);
        strDay = dow.getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
        System.out.println(strDay);

        // Alternatively
        strDay = date.format(DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH));
        System.out.println(strDay);
        strDay = date.format(DateTimeFormatter.ofPattern("EEE", Locale.ENGLISH));
        System.out.println(strDay);
    }
}

Output:

WEDNESDAY
Wednesday
Wed
Wednesday
Wed

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

See the JavaDoc of the DAY_OF_WEEK field. It points to 7 constants SUNDAY..SATURDAY that show how to decode the int return value of cal.get(Calendary.DAY_OF_WEEK). Are you sure that

Calendar cal = Calendar.getInstance();
cal.set(2011, 02, 28);
cal.get(Calendar.DAY_OF_WEEK);

returns the wrong value for you?

Sebastian Zarnekow
  • 6,609
  • 20
  • 23
  • I can assure you, that this is working ;) scala> import java.util.Calendar scala> val cal=Calendar.getInstance() scala> cal.set(2011, 02, 28) scala> cal.get(Calendar.DAY_OF_WEEK) res1: Int = 2 – Martin Thurau Apr 27 '11 at 06:37
1

Try following:

    Calendar cal=Calendar.getInstance();
    int val = cal.get(Calendar.DAY_OF_WEEK);
    System.out.println(new DateFormatSymbols().getWeekdays()[val]);

or

    Calendar cal=Calendar.getInstance();
    String dayName = new DateFormatSymbols().getWeekdays()[cal
            .get(Calendar.DAY_OF_WEEK)];
    System.out.println(dayName);
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
1
Calendar cal=Calendar.getInstance();
    cal.set(2011, 2, 28);
     int val = cal.get(Calendar.DAY_OF_WEEK);
     System.out.println(val);
Dinash
  • 3,027
  • 4
  • 32
  • 45
0

Look at SimpleDateFormat and propably Locale.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
0

If you need the exact date value in the month you need to use Calendar:DAY_OF_MONTH it will return the exact date in the month starting from 1.

//Current date is 07-06-2021 and this will return 7
Calendar cal = Calendar.getInstance();
int val = cal.get(Calendar.DAY_OF_MONTH);
System.out.println("Date in month:"+val);

//If you want the day of the week in text better use the 
//SimpleDateFormat, since Calendar API will return the integer value in 
// the week if we given Calendar.DAY_OF_WEEK
String dayWeekText = new SimpleDateFormat("EEEE").format(cal.getTime());
System.out.println("Day of week:"+dayWeekText);

As suggested in the comment Java Date API have all these feature available. Java 8 introduced new APIs for Date and Time to address the shortcomings of the older java.util.Date and java.util.Calendar.

The same can be achieved using Date API in Java

LocalDate localDate = LocalDate.parse("2021-06-08"); //2021 June 08 Tuesday
System.out.println(localDate.dayOfWeek().getAsText()); //Output as : Tuesday
System.out.println(localDate.dayOfWeek().getAsShortText()); //Output as : Tue
System.out.println(localDate.dayOfMonth().get()); //Output as current date
rTom
  • 11
  • 3
  • The classes `Calendar`and `SimpleDateFormat` that you use are poorly designed and for that reason were officially replaced by [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) in 2014. What is the point in suggesting their use now, 7 years later? – Ole V.V. Jun 08 '21 at 03:11