Java Problem

p3n

Gawd
Joined
Sep 29, 2002
Messages
631
We were told to write this program which takes the date and tells you which day of the week it is:
Code:
import java.util.*;
import java.text.*;
import java.io.*;

class FindDayOfWeek {

	public static void main( String[] args ) throws IOException {
		
		String inputStr;
		int year, month, day;
		
		GregorianCalendar cal;
		SimpleDateFormat sdf;
		
		BufferedReader bufReader;
		
		bufReader = new BufferedReader(
					new InputStreamReader( System.in) );
	
		System.out.print("Year (yyyy): ");
		inputStr = bufReader.readLine();
		year     = Integer.parseInt(inputStr);
		
		System.out.print("Month (1-12): ");
		inputStr = bufReader.readLine();
		month    = Integer.parseInt(inputStr);
				
		System.out.print("Day (1-31): ");
		inputStr = bufReader.readLine();
		day     = Integer.parseInt(inputStr);
		
		cal = new GregorianCalendar(year, month-1, day);
		sdf = new SimpleDateFormat("EEEE");
		
		System.out.print("");
		System.out.print("Day of Week: " + sdf.format(cal.getTime()));
		
		
	}

}
Then change it so it only had one input, easy enough but no when the input is in mm/dd/yyyy format, you need to remove the slashes to get it to work, I thought I could but The prog doesnt work properly, anyway this is what ive tried:
Code:
import java.util.*;
import java.text.*;
import java.io.*;

class FindDayOfWeekOneInput {

	public static void main( String[] args ) throws IOException {
		
		String inputStr;
		int month, day, year;
		
		GregorianCalendar cal;
		SimpleDateFormat sdf;	
			
		BufferedReader bufReader;	
			
		bufReader = new BufferedReader( 
					new InputStreamReader( System.in));		
		String monthStr;
		String dayStr;
		String yearStr;
		
		System.out.print("Enter the Date mm/dd/yyyy: ");
		inputStr = bufReader.readLine();
			
		monthStr = inputStr.substring(0,2);
			
		dayStr = inputStr.substring(3,5);
			
		yearStr = inputStr.substring(6,10);
		
		
		month = Integer.parseInt(monthStr);
		day = Integer.parseInt(dayStr);
		year = Integer.parseInt(yearStr);
		
		cal = new GregorianCalendar(month, day, year);
		sdf = new SimpleDateFormat("EEEE");
		
		System.out.print("");
		System.out.print("Day of Week: " + sdf.format(cal.getTime()));
	}

}
Is there some easy way of doing this, am I on the right track ?!

I've modified the prog to print what the strings contain and it showed '02262004' .. which should work .. aahhh!

help!

*edit this soulda been a reply close plz!
 
from first program:

cal = new GregorianCalendar(year, month-1, day);

from second program:

cal = new GregorianCalendar(month, day, year);


does the order have anything to do with it? i would imagine so.
 
It's amazing how things break when you're not following the API...
 
The other option to break apart the string of the format mm/dd/yyyy, where mm and dd can be 1 or two digits, is use a StringTokenizer and tokenize on "/".
 
Back
Top