I have a program that asks the user to enter a date in numeric form, the program
ID: 3656596 • Letter: I
Question
I have a program that asks the user to enter a date in numeric form, the program then converts the date to the name of the month, day, and year. It also gives what day of the year it is. I have the program working flawlessly but I need it to work like it does, only it has to use the Gregorian Calendar. PLEASE help! Here is what I have (although I know it will have to nearly be completely redone):
import java.util.Scanner;
import java.util.regex.MatchResult;
import javax.swing.JOptionPane;
public class ConvertDate
{
static final int MONTH = 0;
static final int DAY = 1;
static final int YEAR = 2;
static final String[] monthName = {
"UNUSED",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
static int[] daysInMonth = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, };
static int[] parseDate (String input)
{
int[] ret = new int[3];
Scanner scanner = new Scanner(input);
scanner.findInLine("(\d+)/(\d+)/(\d+)");
try {
MatchResult result = scanner.match();
ret [MONTH] = Integer.parseInt(result.group(1));
ret [DAY] = Integer.parseInt(result.group(2));
ret [YEAR] = Integer.parseInt(result.group(3));
return ret;
}
catch (Exception ex) {
return null;
}
}
public static void main(String[] args) {
String response;
int retdate [] = null;
while (true)
{
response = JOptionPane.showInputDialog(null, "Enter date:");
if (response == null)
System.exit(0);
retdate = parseDate(response);
if (retdate == null)
continue;
if (retdate[YEAR] < 0)
continue;
if (retdate[YEAR] % 4 == 0) {
if (retdate[YEAR] % 100 == 0) {
if (retdate[YEAR] % 400 == 0)
daysInMonth[2] = 29;
else
daysInMonth[2] = 28;
}
else
daysInMonth[2] = 29;
}
else
daysInMonth[2] = 28;
if (retdate[MONTH] < 1 || retdate[MONTH] > 12)
continue;
if (retdate[DAY] < 1 || retdate[DAY] > daysInMonth[retdate[MONTH]])
continue;
break;
}
String result1 = "Date is " + monthName[retdate[MONTH]] + " " +
retdate[DAY] + ", " + retdate[YEAR] + " ";
int dayInYear = 0;
for (int i = 1; i < retdate[MONTH]; i++)
dayInYear += daysInMonth[i];
dayInYear += retdate[DAY];
result1 += "Day in year is " + dayInYear;
System.out.println (result1);
JOptionPane.showMessageDialog(null, result1, "message",
JOptionPane.INFORMATION_MESSAGE);
}
}
Explanation / Answer
package com.tutorialspoint; import java.util.*; public class GregorianCalendarDemo { public static void main(String[] args) { // create a new calendar GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); // print the current date and time System.out.println("" + cal.getTime()); // add 2 months cal.add((GregorianCalendar.MONTH), 2); // print the modified date and time System.out.println("" + cal.getTime()); // add 2 years cal.add((GregorianCalendar.YEAR), 2); // print the modified date and time System.out.println("" + cal.getTime()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.