// Imports
import java.
*;
import java.
awt.
*;
import java.
io.
*;
import java.
util.
*;
import java.
lang.
*;
import java.
text.
*;
class DateParse
{
// * Variables
public String[] m_strMonths =
{ "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" };
// * Constructor
public DateParse
()
{
}
// * Main
public static void main
(String args
[])
{
// Declare variables
DateParse dpParse =
new DateParse
();
// Run
dpParse.
Run();
// Exit
System.
exit(1);
}
// * Run
public void Run
()
{
// Declare variables
NumberFormat nfFormat =
NumberFormat.
getInstance();
StreamTokenizer stTokens =
new StreamTokenizer(new BufferedReader(new InputStreamReader(System.
in)));
String strInput =
new String("");
BufferedReader brInput =
new BufferedReader(new InputStreamReader(System.
in));
// Setup tokenizer
stTokens.
eolIsSignificant(true);
// Prompt
System.
out.
print("Enter a date in form MM/DD/YY or MM DD, YYYY: ");
// Try
try
{
// -- Get user input
strInput = brInput.
readLine();
// -- Parse
Parse
(strInput
);
}
catch (IOException ioExcept
)
{
System.
out.
println("\n\nError: " + ioExcept.
toString() +
"!\n\n");
System.
exit(1);
}
}
// * Handle invalid input
public void InvalidInput
()
{
System.
out.
println("\n\nError: Invalid input.\n\n");
System.
exit(1);
}
// * Parse
public void Parse
(String strInput
)
{
// Declare variables
// Validate
if (strInput.
length() ==
0)
{
InvalidInput
();
}
// See what mode we are in
if (IsNumeric
(strInput.
charAt(0)) ==
true)
{
ParseNumeric
(strInput
);
}
else
{
ParseString
(strInput
);
}
}
// * Determines if a character is numeric
public boolean IsNumeric
(char cChar
)
{
// Validate
return Character.
isDigit(cChar
);
}
// * Determines if a string is numeric
public boolean IsNumeric
(String strString
)
{
// Validate
for (int i =
0; i < strString.
length(); i++
)
{
if (Character.
isDigit(strString.
charAt(i
)) ==
false)
{
return false;
}
}
return true;
}
// * Parse numeric
public void ParseNumeric
(String strInput
)
{
// Declare variables
StringBuffer strBuffer =
new StringBuffer(strInput
);
StringBuffer strOutput =
new StringBuffer("");
String strTemp;
int nFindStart =
0, nFindEnd =
0;
Integer nTemp;
// Get the month
nFindEnd = strBuffer.
indexOf("/", nFindStart
);
if (nFindEnd ==
-1)
{
InvalidInput
();
}
strTemp = strBuffer.
substring(nFindStart, nFindEnd
);
// -- Validate
if (IsNumeric
(strTemp
) ==
false)
{
InvalidInput
();
}
if (ValidateMonth
(new Integer(strTemp
)) ==
false)
{
InvalidInput
();
}
// -- Convert month
nTemp =
new Integer(strTemp
);
strOutput.
append(ConvertMonth
(nTemp.
intValue()) +
" ");
// Switch start and end
nFindStart = nFindEnd +
1;
// Get day
nFindEnd = strBuffer.
indexOf("/", nFindStart
);
if (nFindEnd ==
-1)
{
InvalidInput
();
}
strTemp = strBuffer.
substring(nFindStart, nFindEnd
);
// -- Validate
if (IsNumeric
(strTemp
) ==
false)
{
InvalidInput
();
}
// -- Append
strOutput.
append(strTemp +
", ");
// Switch start and end
nFindStart = nFindEnd +
1;
// Get year
strTemp = strBuffer.
substring(nFindStart
);
// -- Validate
if (IsNumeric
(strTemp
) ==
false)
{
InvalidInput
();
}
nTemp =
new Integer(strTemp
);
// -- Append
if (strTemp.
length() ==
2)
{
if (nTemp.
intValue() <=
2)
{
strOutput.
append("20" + strTemp
);
}
else
{
strOutput.
append("19" + strTemp
);
}
}
else
{
strOutput.
append(strTemp
);
}
// Output
System.
out.
println("\n\n" + strOutput.
toString() +
"\n\n");
}
// * Parse string
public void ParseString
(String strInput
)
{
// Declare variables
StringBuffer strBuffer =
new StringBuffer(strInput
);
StringBuffer strOutput =
new StringBuffer("");
String strTemp;
int nFindStart =
0, nFindEnd =
0;
Integer nTemp;
// Get the month
nFindEnd = strBuffer.
indexOf(" ", nFindStart
);
if (nFindEnd ==
-1)
{
InvalidInput
();
}
strTemp = strBuffer.
substring(nFindStart, nFindEnd
);
// -- Validate
nTemp =
new Integer(ValidateMonth
(strTemp
));
if (nTemp.
intValue() ==
-1)
{
InvalidInput
();
}
// -- Append
strOutput.
append(nTemp.
toString() +
"/");
// Switch start and end
nFindStart = nFindEnd +
1;
// Get day
nFindEnd = strBuffer.
indexOf(", ", nFindStart
);
if (nFindEnd ==
-1)
{
InvalidInput
();
}
strTemp = strBuffer.
substring(nFindStart, nFindEnd
);
// -- Append
strOutput.
append(strTemp +
"/");
// Switch start and end
nFindStart = nFindEnd +
2;
// Get year
strTemp = strBuffer.
substring(nFindStart
);
// -- Validate
if (IsNumeric
(strTemp
) ==
false)
{
InvalidInput
();
}
// -- Append
strOutput.
append(strTemp
);
// Output
System.
out.
println("\n\n" + strOutput.
toString() +
"\n\n");
}
// * Convert month
public String ConvertMonth
(int nMonth
)
{
// Convert
return m_strMonths
[nMonth -
1];
}
// * Validate month
public boolean ValidateMonth
(Integer nMonth
)
{
// Validate
if (nMonth.
intValue() >=
1 || nMonth.
intValue() <=
12)
{
return true;
}
return false;
}
// * Validate month
public int ValidateMonth
(String strMonth
)
{
// Go through all months
for (int i =
0; i < m_strMonths.
length; i++
)
{
if (m_strMonths
[i
].
equalsIgnoreCase(strMonth
) ==
true)
{
return i +
1;
}
}
return -1;
}
}