Question Index

Write a program to create the Time String for the distance between two dates. In the string Y represents Years, M for Months, W for Weeks and D for Days. If start date is greater than end date, then return -1.

Input (String, String) Output (String)
07-Feb-2002, 01-Jan-2012 9Y-10M-3W-4D
28-Jan-2000, 01-Mar-2000 0Y-1M-0W-1D
15-Aug-2001, 15-July-2002 0Y-11M-0W-0D
28-Mar-2000, 01-Mar-2000 -1

class CreateTimeDistanceString

{    public static void main(String s[])
        String startDate = "28-Feb-2000", endDate = "01-Mar-2000";
        String timeString = createTimeDistanceString(startDate, endDate);
        System.out.println("The Time String is " + timeString);


public static String createTimeDistanceString(String first, String last) {
//Write code here to create the time distance string in the required format

//If required, write any additional methods here



Topic: Locale Class In Java

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App