stuck on writing Date application
I have to write an application to convert a date entered from the keyboard in the format 5/06/2000 to May 5, 2000 and then display the day of the year. I have to use arrays to store the names of the months as well as the days in each month. This is the code I have so far, but I don't know how proceed after the user inputs the date. How do I begin converting the input date from one format to another?
import java.util.Scanner;
import java.util.*;
public class ConvertDate
{
public static void main(String[] args)
{
String aDate;
String[] month = {"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
int[] numberOfDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Scanner input = new Scanner(System.in);
System.out.println("Please enter a date using the MM/DD/YYYY format:");
aDate = input.nextLine();
//need to find how to convert the input to the Month name
//need to find a way to use the Gregorian Calendar to
//convert the input day and month to the day of the year
}
}
Re: stuck on writing Date application
SimpleDateFormat (Java Platform SE 6)
GregorianCalendar (Java Platform SE 6)
If you can use other classes, you could use the SimpleDateFormatter with the pattern "MM/dd/yyyy" and the method parse to parse the aDate-string to an date object (otherwise: you could use the split method of the string class and Integer.parseInt)
//need to find a way to use the Gregorian Calendar to
create a gregoriancalendar object and setTime and pass the date object
//need to find how to convert the input to the Month name
month[gregoriancalendarobject.get(Calendar.MONTH)] , see API-doc
//convert the input day and month to the day of the year
gregoriancalendarobject.get(Calendar.DAY_OF_YEAR) , see API-doc
Re: stuck on writing Date application
See the Calendar and DateFormat classes. They have methods that will help you.