1185. Day of the Week

1185. Day of the Week

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Solution {
	  	public String dayOfTheWeek(int day, int month, int year) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = format.parse(year + "-" + ((month <= 9) ? ("0" + month) : ("" + month)) + "-" + ((day <= 9) ? ("0" + day) : ("" + day)));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int d = date.getDay();
		String dayString = null;
		switch (d) {
		case 1:
			dayString = "Monday";
			break;
		case 2:
			dayString = "Tuesday";
			break;
		case 3:
			dayString = "Wednesday";
			break;
		case 4:
			dayString = "Thursday";
			break;
		case 5:
			dayString = "Friday";
			break;
		case 6:
			dayString = "Saturday";
			break;
		case 0:
			dayString = "Sunday";
			break;
		default:
			dayString = "Invalid day";
			break;
		}

		return dayString;
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s