Question

In: Computer Science

JAVA Using the java.util.Calendar class, create 3 dates and times. Write a method to identify and...

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.

Solutions

Expert Solution

=============================================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




Related Solutions

Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
(Using Java) create a class that can identify a palindrome. A palindrome is defined as -...
(Using Java) create a class that can identify a palindrome. A palindrome is defined as - A string of characters that reads the same from left to right as its does from right to left - Example: Anna, Civic, Kayak, Level, Madam - To recognize a palindrome, a queue can be used in conjunction with a stack o A stack can be used to reverse the order of occurrences o A queue can be used to preserve the order of...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
In java please create a class with a method to be able to add ints into...
In java please create a class with a method to be able to add ints into a linked List and to be able to print out all the values at the end. public class Node { int data; Node next;
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file,...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report. Project Description You’ll read and write files containing objects of the Loan class. Here are the details of that class: Instance Variables: customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) Methods: getters for all instance variables setters for all instance variables...
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT