CODING IN JAVA
Dates are printed in several common formats. Two of the more common formats are:
07/21/55 and July 21, 1955
Write a program that reads a date in the first format and prints
that date in the second format.
Prompt the user and accept user input for the date in MM/DD/YYYY
format.
Retrieve the month portion of the date entered and convert it to an
integer.
Do the same thing for day and year
Create a string named months that contains the number of the month
and the name of the month: 1 January2 February3 March….
Convert the month into a string
Find the index where the numeric value for the month is held
Retrieve the text version of the month.
Output the date in expanded format.
//Sample run #1
Enter a date in MM/DD/YYYY format: 04/12/2000
April 12, 2000
//Sample run #2
Enter a date in MM/DD/YYYY format: 11/10/2000
November 10, 2000
//Sample run #3
Enter a date in MM/DD/YYYY format: 12/20/2000
December 20, 2000
Note that for sample run 3, you are likely to get an out of bounds
error unless you provide a 13th option within the string.
Exception in thread "main"
java.lang.StringIndexOutOfBoundsException: begin 90, end -1, length
98
at
java.base/java.lang.String.checkBoundsBeginEnd(String.java:3107)
at
java.base/java.lang.String.substring(String.java:1873)
at edu.ilstu.DateProgram.main(DateProgram.java:20)
CODE:
import java.io.*;
//class Date
public class Date{
//main method
public void main(String args[])throws IOException{
//strigng to hold the input
String input;
//integer to store the month date and year
int m,d,y;
//String months that holds the month date and month name
String months = "1 January 2 February 3 March 4 April 5 May 6 June
7 July 8 August 9 September 10 October 11 November 12 December
13";
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
//Asking the user to input the date
System.out.println("Enter a date in MM/DD/YYYY format");
input = in.readLine();
//using the .split() method to get the month date and year
strings
String[] date = input.split("/");
//converting the strings to integers
m = Integer.parseInt(date[0]);
d = Integer.parseInt(date[1]);
y = Integer.parseInt(date[2]);
//checking if the date and month entered was valid or not
if(m > 12||m <1||d>31||d<1)
{
System.out.println("Invalid Month or Date");
}
//if it was valid
else{
//the index of the month number is found and the index of the next
month is found out and the substring between these two indeces
gives the month string
date[0] =
months.substring(months.indexOf(Integer.toString(m))+2,months.indexOf(Integer.toString(m+1))).trim();
//printing the month date and year in the second fromat
System.out.println(date[0]+" "+date[1]+", "+date[2]);
}
}
}
CODE IMAGE(Check the indentation):
OUTPUT:
________________________________________________________________
Feel to free to ask any questions in the comments section to get your doubts cleared.
Thank You!
CODING IN JAVA Dates are printed in several common formats. Two of the more common formats...
Python 8.20 (Regular Expressions: Munging Dates) Dates are stored and displayed in several common formats. Three common formats are 042555 04/25/1955 April 25, 1955 Use regular expressions to search a string containing dates, find substrings that match these formats and munge them into other formats. The original string should have one date in each format, so there will be a total of six transformations. This question is taken directly from the textbook.
I need this in Java, please! The ISO 8601 Standard date format for Information Interchange indicates that a date be written as such: yyyy-MM-dd (eg. 2012-07-02, 1999-12-05, 1998 -01-27 ) where yyyy represents the four digit year MM represents a two digit numerical month dd represents a two digit numerical day Chinese date format is specified as: yyyy-M-d Macedonean date format is specified as: d.M.yyyy where yyyy represents the four digit year M represents a one or two digit numerical...
Finish the program. Follow the program logic and then complete the algorithm in the function. Use the programs given constructs and variables only. #include <iostream> #include <string> using namespace std; // Prototype string format(string); int main() { // The following string will hold the user's input. string userDate; // The following string will hold the formatted date. string formattedDate; // Get a date from the user. cout << "Enter a date in the form mm/dd/yyyy: "; cin >> userDate; //...
In Java please Project 14-1: Reservation Calculator Create an application that gets arrival and departure dates for a reservation and calculates the total amount for the stay. Console Reservation Calculator Enter the arrival month (1-12): 5 Enter the arrival day (1-31): 16 Enter the arrival year: 2018 Enter the departure month (1-12): 5 Enter the departure day (1-31) : 18 Enter the departure year: 2018 Arrival Date: May 16, 2018 Departure Date: May 18, 2018 Price: $145.00 per night Total...
anyString.substr(x, n) - Returns a copy of a substring. The substring is n characters long and begins at position x of anyString. Write a program that reads a string from the user containing a date in the format mm/dd/yyyy. You have to use above substring method to extract the various fields from the format. It should print the date in the form Month Date, Year. Validate: Exit the program with error message as “Invalid date format” if length of...
Julian day numbers (JDN) are used to provide a number for a day of the year so that calculations can be performed relatively easily to determine the difference between two dates. The JDN for a day is the number of days that have elapsed between that day and a base date of January 1, 4713 BC. Use the following calculation: JDN = (1461 × (Y + 4800 + (M − 14) / 12)) / 4 + (367 × (M −...
C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook: A formula can be used to calculate the number of days between two dates. This is affected by...
THE PROBLEM:It makes it easier if you use switch statements for some part of the homework. Write a program that inputs a month, day, and year from the user and outputs the corresponding date in the following two standard date formats:6/12/2005 June 12,2005Also your program must print the name of any holiday associated with the date.For example:3/17/2010 March 17, 2010 St. Patrick's Day Your program should ask the user how many times the user wants to run the code and then...
Q2. Retrieve the names of all employees from the employee table to produce output on CSV format or delimited format with a common delimeter, rather than separete columns. Hint:Put the whole row into a string with a semicolon as the seperator(delimeter) between thecolumns: FORMAT:(fname;minit;lname) Example: EMPLOYEES -------------- James;E;Borg Frank;T;Wong Q3. Write a query to show the employees name from the employee table in this format: first letter of the first name, followed by a dot, a blank, and the full...