my i have wrote my code is like this but m not to get get proper out put
here is my code

System.out.println("Enter Date of Birth dd/mm/yyyy");
DataInputStream in=new DataInputStream(System.in);
String dob= in.readLine();
String[] dob1=dob.split("/");

	int year=Integer.parseInt(dob1[2]);
	int month=Integer.parseInt(dob1[1]);
	int date=Integer.parseInt(dob1[0]);
	System.out.println("Date : "+date+ "Month : "+month+" year :"+year);
	Calendar cal = new GregorianCalendar(year,month,date);

	int dayOfWeek =cal.DAY_OF_WEEK;   
	switch(dayOfWeek){
	case 1:System.out.println("Sunday");break;
	case 2:System.out.println("Monday");break;
	case 3:System.out.println("Tuesday");break;
	case 4:System.out.println("Webnesday");break;
	case 5:System.out.println("Thursday");break;
	case 6:System.out.println("Friday");break;
	case 7:System.out.println("Saturday");break;
	}

out put:
Enter Date of Birth dd/mm/yyyy
21/11/1985
Date : 21Month : 11 year :1985
Saturday
wich is total wrong ans please help to for this

Recommended Answers

All 3 Replies

Your int DAY_OF_WEEK is just the "code" value used in Calendar to refer to the day-of-week field. You then need to get that particular field from your Calendar using the get(int field) method.

ps: using a switch like that will work, but it's a pretty horrible way to do it.
Much better to define an array of day names and use the day number as an index into the array

Your int DAY_OF_WEEK is just the "code" value used in Calendar to refer to the day-of-week field. You then need to get that particular field from your Calendar using the get(int field) method.

ps: using a switch like that will work, but it's a pretty horrible way to do it.
Much better to define an array of day names and use the day number as an index into the array

forgive for sounding like a noob (i am new to programming :) ) but how is defining an array of day names and using the day number as an index a more efficient method?

final static String[] dayNames = {"Sunday", "Monday ...
// (done just once)
...

int dayOfWeek = cal.get(...
System.out.println(dayNames[dayOfWeek]);
// each time the conversion from number to name is needed

Code is shorter and more efficient (one array index vs multiple tests and repeated code in the switch)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.