Date Time Time Difference – – import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; import java.lang.System; import java.time.Duration; import java.time.Instant; public class DateTime { static Date d1, d2; public static void main(String[] args) { new Scanner in = Scanner(System.in); System.out.println("Enter 1 to Book a bicycle or 2 to Return a bicycle or 3 Exit"); int userChoice = in.nextInt(); while (userChoice != 3){ switch (userChoice) { case 1: bookBicycle(); case 2: returnBicycle(); } System.out.println("Enter 1 to Book a bicycle or 2 to Return a bicycle or 3 Exit"); userChoice = in.nextInt(); } } static void bookBicycle(){ new d1 = Date(); } static void returnBicycle(){ new d2 = Date(); System.out.println(d2.getTime()-d1.getTime()); //difference in milliseconds. System.out.println((d2.getTime()-d1.getTime())/1000); } }
Date – Time – Time Difference import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; import java.lang.System; import java.time.Duration; import java.time.Instant; public class DateTime { static Date d1, d2; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter 1 to Book a bicycle or 2 to Return a bicycle or 3 Exit"); int userChoice = in.nextInt(); while (userChoice != 3){ switch (userChoice) { case 1: bookBicycle(); case 2: returnBicycle(); } System.out.println("Enter 1 to Book a bicycle or 2 to Return a bicycle or 3 Exit"); userChoice = in.nextInt(); } } static void bookBicycle(){ d1 = new Date(); } static void returnBicycle(){ d2 = new Date(); System.out.println(d2.getTime()-d1.getTime()); //difference in milliseconds. System.out.println((d2.getTime()-d1.getTime())/1000); } }