Find name of day by year, month and day [duplicate]

Use SimpleDateFormat with a pattern of EEEE to get the name of the day of week.

// Assuming that you already have this.
int year = 2011;
int month = 7;
int day = 22;

// 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

Here it is wrapped all up into a nice Java class for you.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class DateUtility
{

    public static void main(String args[]){
        System.out.println(dayName("2015-03-05 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);
    }
}

Leave a Comment