Below is the assignement and I have pretty much made the program work but it mes
ID: 665881 • Letter: B
Question
Below is the assignement and I have pretty much made the program work but it messes up on the name portion of the date when the message appears. Its also in a loop where it spits out more dates every time you press enter or "ok" button.
//8. Write an application in which the user can enter a date using digits and slashes
// (for example, “6/24/2014”), and receive output that displays the date with the month
// shown as a word (such as “June 24, 2014”). Allow for the fact that the user might or
// might not precede a month or day number with a zero (for example, the user might
// type “06/24/2014” or “6/24/2014”). Do not allow the user to enter an invalid date,
// defined as one for which the month is less than 1 or more than 12, or one for
// which the day number is less than 1 or greater than the number of days in the
// specified month. Also display the date’s ordinal position in the year; for example,
// 6/24/14 is the 175th day of the year. In this application, use your knowledge of arrays
// to store the month names, as well as values for the number of days in each month
// so that you can calculate the number of days that have passed. Save the application
// as ConvertDate.java.
//Exercise 8 Additional Requirements
// Use the GregorianCalendar class to determine the date's ordinal position in the year.
import javax.swing.*;
import java.util.*;
public class ConvertDate{
public static void main(String[]args){
String[] monthNum = {"01", "02","03","04","05","06","07","08","09","10","11","12"};
String[] monthName = {"January","February","March","April","May","June","July","August","September","October","November","December"};
String date;
date = JOptionPane.showInputDialog(null, "Enter date as Follows:" , "MM/DD/YYYY");
int[] daysPerMonth={31,28,31,30,31,30,31,31,30,31,30,31};
String [] dateFormats = date.split("/");
int month, day, year;
month = Integer.parseInt(dateFormats[0]);
day = Integer.parseInt(dateFormats[1]);
year = Integer.parseInt(dateFormats[2]);
for(int i=0; i<dateFormats.length; i++){
JOptionPane.showMessageDialog(null, "Date: "+monthName[i]+" "+day+", "+year);
}
GregorianCalendar gc = new GregorianCalendar();
Date d = new Date(date);
gc.setTime(d);
JOptionPane.showMessageDialog(null,"Day of Year: "+gc.get(GregorianCalendar.DAY_OF_YEAR));
}
}
Explanation / Answer
//This should work just fine
import javax.swing.*;
import java.util.*;
public class ConvertDate{
public static void main(String[]args){
String[] monthNum = {"01", "02","03","04","05","06","07","08","09","10","11","12"};
String[] monthName = {"January","February","March","April","May","June","July","August","September","October","November","December"};
String date;
date = JOptionPane.showInputDialog(null, "Enter date as Follows:" , "MM/DD/YYYY");
int[] daysPerMonth={31,28,31,30,31,30,31,31,30,31,30,31};
String [] dateFormats = date.split("/");
int month, day, year;
month = Integer.parseInt(dateFormats[0]);
day = Integer.parseInt(dateFormats[1]);
year = Integer.parseInt(dateFormats[2]);
JOptionPane.showMessageDialog(null, "Date: "+monthName[month-1]+" "+day+", "+year);
GregorianCalendar gc = new GregorianCalendar();
Date d = new Date(date);
gc.setTime(d);
JOptionPane.showMessageDialog(null,"Day of Year: "+gc.get(GregorianCalendar.DAY_OF_YEAR));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.