I need help with this java question. The thing is I could do it, but I do not kn
ID: 3716651 • Letter: I
Question
I need help with this java question.
The thing is I could do it, but I do not know how to accept a whole line of input like 05/04/2018 and store it in 3 int variables (like month, date and year).
How do i split input with "/"?
OR Do I accept a whole line as a String and split it into 3 different strings then convert them to integers for input validation? That sounds too complicated.
OR Do I accept them as characters? And do the same as with string?..
A note: the program should be accomplished without any date or calendar library imports. It should be done with pretty much simple methods.
I need to accept an input line like this "05/04/2018" and store them in mm, dd and yyyy accordingly as integers (so I can do input validation later).
If you help me with that, the rest I could do easily. But I would appreciate the whole solution as well. Many Thanks.
QUESTION:
-------------------------------------------------------------------------
Dates are printed in several common formats. Two of the more common formats are:
04/12/2018 and April 12,2018
Write a program that reads a date in the first format and prints it in the second format.
Do not accept invalid date format.Run your program with the following data:
04/12/2018
13/01/2000
02/29/2015
01/01/2014
09/31/2016
---------------------------------------------------------------------
Explanation / Answer
Solution:
code:
import java.text.SimpleDateFormat;
import java.util.*;
public class MyDate {
public static void main(String args[]) {
try
{
Scanner in = new Scanner(System.in);
//Reading the date to a string
System.out.println(" Please Enter the date : ");
String datee = in.next();
//setting the string to date of the format mm/dd/yyyy
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
formatter.setLenient(false);
Date date = formatter.parse(datee);
//converting the date in to requried format
SimpleDateFormat formatter1 = new SimpleDateFormat("MMMM dd, yyyy");
formatter1.setLenient(false);
String strDate = formatter1.format(date);
//printing the date in requried format
System.out.println(" Date in the requried Format is : "+strDate);
}
catch(Exception e)
{
System.out.println(" Invalid Date!!");
}
}
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)