In: Computer Science
JAVA Using the java.util.Calendar class, create 3 dates and times. Write a method to identify and print out the oldest of the 3 timestamps. Likewise, create another method that prints out the most recent time.
=============================================Code==================================
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.*;
import java.util.Calendar;
/* Name of the class has to be "Main" only if the class is
public. */
class Codechef
{
public static void main (String[] args) throws
java.lang.Exception
{
//Creating 3 Date and Time and comparing the
oldest
OldestTimeStamp();
//Creating 3 Date and Time and
comparing the recent
RecentTimeStamp();
}
public static void OldestTimeStamp()
{
//Takes the format of date and time
SimpleDateFormat dateformat = new
SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
try
{
//Creating Date and Time
Date date1 = dateformat.parse("15/01/1989 11:35:42");
Date date2 = dateformat.parse("13/09/1994 00:00:00");
Date date3 = dateformat.parse("13/02/1995 13:59:59");
//Printing Date and Time
System.out.println("Date1 is: "+dateformat.format(date1));
System.out.println("Date2 is: "+dateformat.format(date2));
System.out.println("Date3 is: "+dateformat.format(date3));
if (date1.after(date2) && date1.after(date3))
{
System.out.println("Date1 is later than Date2 and Date 3");
}
else if(date2.after(date1) && date2.after(date3))
{
System.out.println("Date2 is later than Date1 and Date 3");
}
else if(date3.after(date1) && date3.after(date2))
{
System.out.println("Date3 is later than Date1 and Date 2");
}
else if(date1.equals(date2) && date2.equals(date3))
{
System.out.println("All are same date");
}
}
catch (ParseException e)
{
e.printStackTrace();
}
}
public static void RecentTimeStamp()
{
//Takes the format of date and time
SimpleDateFormat dateformat = new
SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
try
{
//Creating 3 Date and Time
Date date1 = dateformat.parse("15/01/1989 11:35:42");
Date date2 = dateformat.parse("13/09/1994 00:00:00");
Date date3 = dateformat.parse("13/02/1995 13:59:59");
//Printing Date and Time
System.out.println("Date1 is: "+dateformat.format(date1));
System.out.println("Date2 is: "+dateformat.format(date2));
System.out.println("Date3 is: "+dateformat.format(date3));
//Comparing Date and Time
if (date1.before(date2) && date1.before(date3))
{
System.out.println("Date1 is before than Date2 and Date 3");
}
else if(date2.before(date1) && date2.before(date3))
{
System.out.println("Date2 is before than Date1 and Date 3");
}
else if(date3.before(date1) && date3.before(date2))
{
System.out.println("Date3 is before than Date1 and Date 2");
}
else if(date1.equals(date2) && date2.equals(date3))
{
System.out.println("All are same date");
}
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
======================================Output===================================================
=============================================================================================
Please upvote and comment for doubts.Thanks