yet again right?

Ok, so this time, the task is to write an application that asks the user for his or her birth date and replies with the day of the week on which they were born.

So here is the code i have.

package Ch2Scanner;
import java.text.SimpleDateFormat;
import java.util.*;
public class Ch2Scanner {
	public static void main(String[] args) {
		Scanner scanner;
		scanner = new Scanner(System.in);
		SimpleDateFormat sdf;
		sdf = new SimpleDateFormat("EEEE");
	
		String birthday;
		//prompt the user for input
	java.util.Date bdate = java.sql.Date.valueOf("1990-07-21");
		System.out.println("Enter your birthday here:");
		
		birthday= scanner.next();
		System.out.println("What is your birthday?" + birthday + "." );
		
	}
}

ok, so when i run it, it comes up with, enter birthday, so i do and hit enter. Then it just reads back what I entered. I need it to say saturday. The TA (who can hardly speak english) said something about using the SimpleDateFormat and creating an object for it, then implementing it, and i thought i had..but i guess not. Any help would be great. Thanks in advanced.

Recommended Answers

All 2 Replies

If you create an instance of SimpleDateFormat using a pattern that corresponds to your intput, you can use the parse(String) method to convert the string input to a Date object, which you can then use to get the day of the week. Calendar is what you should really use for this, but I'm not sure what the specifics of your assignment are (and the whole date/calendar thing is pretty much a mess anyway).

With this: java.util.Date bdate = java.sql.Date.valueOf("1990-07-21");
you can get the value of a string, so get the value of the input from the user.

Then you can use this value to get the day (represented as an integer):
bdate.getDate() .

Thats pretty much it, if you want the output to be a little better create an array of strings, like: String days[] = {"Sunday", "Monday" etc..}

and then get this day with: days[bdate.getDays]

However, getDate() is deprecated so you might want to look up how to solve the problem with Calendar instead.

Hope this helps!

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.