Enter Date 6 24 2014 Get Following Output Date June 24 201 Day Year 175 Enter Date 6 24 14 Q10055855
If I enter date as 6/24/2014 I get the following output:
Date is June 24, 201
Day in the year is: 175
If I enter date as 6/24/14 I get the following output:
Date is June 24, 1
Day in the year is: 175
Not sure what isn’t being calculated correctly for year.
Also, not really sure how to account for leap year per theProgram requirements.
___________________________________________________________________________
Program requirements:
Write an application in which the user can enter a date usingdigits and slashes (for example,“6/24/2014”), and receive outputthat displays the date with the month shown as a word (such as“June24, 2014”). Allow for the fact that the user might or might notprecede a month or day number with a zero (for example, the usermight type“06/24/2014”or“6/24/2014”). Do not allow the user toenter an invalid date, defined as one for which the month is lessthan 1 or more than 12, or one for which the day number is lessthan 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 yourknowledge of arrays to store the month names, as well as values forthe number of days in each month so that you can calculate thenumber of days that have passed. Save the application asConvertDate.java.
When determining whether a date is valid and when calculatingthe number of days that have passed, remember that some years areleap years. In a leap year, February 29th is added to the calendar.A leap year is any year that is evenly divisible by 4, unless theyear is also evenly divisible by 100. So 1908 and 2008 were bothleap years, but 1900 was not a leap year. Another exception occurswhen a year is evenly divisible by 400—the year is a leap year.Therefore, 2000 was a leap year, but 2100 will not be one.
____________________________________________________________________________
import javax.swing.*;
import java.util.*;
public class ConvertDate
{
public static void main(String[] args)
{
String date = “”;
String monthNum = “”;
String monthDay = “”;
String year = “”;
int one;
int two;
int m; //month
int d; //day
int y; //year
int[] daysInMonth = {31, 28, 31, 30,31, 30, 31, 31, 30, 31, 30, 31};
String[] month = {“January”,”February”, “March”, “April”, “May”, “June”, “July”, “August”,”September”, “October”, “November”, “December”};
date =JOptionPane.showInputDialog(null, “Please enter the date”);
one = date.indexOf(“/”);
monthNum = date.substring(0,one);
date = date.substring(one + 1,date.length() – one + 1);
one = date.indexOf(“/”);
monthDay = date.substring(0, one);
year = date.substring(one + 1,date.length() – one + 1);
m =Integer.parseInt(monthNum);
d =Integer.parseInt(monthDay);
y = Integer.parseInt(year);
if ((m >= 1) && (m <12))
{
if ((d > 1)&& (d <= daysInMonth[m-1]))
{
GregorianCalendar calendar = new GregorianCalendar(y, m-1,d);
int dayOfYear = calendar.get(GregorianCalendar.DAY_OF_YEAR);
JOptionPane.showMessageDialog(null,”Date is ” + month[m-1] + ” ” +d + “, ” + y
+ “nDay in the year is: ” + dayOfYear);
}
else
{
System.out.println(“Please enter a valid date”);
}
}
else
{
System.out.println(“Please enter a valid date”);
}
}
}