Search code examples
javadatetimejava.util.date

Adding time from a string to a Java date: too little is added


I have a string that is usually in format "HH:mm:ss", but can also appear as "x days HH:mm:ss". I have to parse it to Date object and I want to calculate what Date will it be after this time from string. How can it be done?

Date now = new Date();
Date delay = new SimpleDateFormat("HH:mm:ss").parse(myString);
Date after = new Date(now.getTime() + delay.getTime());

But unfortunately it doesn't work. It adds, but very small value as "delay".


Solution

  • Highlights:

    • Use String.split to determine if you have a time or a duration and a time.
    • assume the date duration starts with the current date.
    • duration will be current date + (duration -1) more days (example does not validate duration).
    • Use Calendar for date stuff.
    • Calendar.add(Calendar.DAY_OF_YEAR, xxx) gives the end date.

    Try something like this:

    public class LearnDate
    {
        private static DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
    
        public static void main(final String[] arguments)
            throws ParseException
        {
            final String input1 = "14:21:15";
            final String input2 = "4 days 15:14:15";
    
            printDateInfo(input1);
            printDateInfo(input2);
        }
    
        private static String formatCalendar(final Calendar input)
        {
            return
                "" +
                input.get(Calendar.DAY_OF_MONTH) +
                monthName(input.get(Calendar.MONTH)) +
                input.get(Calendar.YEAR) +
                " " +
                timeFormat.format(input.getTime());
        }
    
        private static String monthName(final int month)
        {
            switch (month)
            {
                case Calendar.JANUARY:
                    return "Jan";
    
                case Calendar.FEBRUARY:
                    return "Feb";
    
                case Calendar.MARCH:
                    return "Mar";
    
                case Calendar.APRIL:
                    return "Apr";
    
                case Calendar.MAY:
                    return "May";
    
                case Calendar.JUNE:
                    return "Jun";
    
                case Calendar.JULY:
                    return "Jul";
    
                default:
                    return "blah";
            }
        }
    
        private static void printDateInfo(final String input)
            throws ParseException
        {
            String[] parsedInput = input.split(" ");
            Date time = null;
    
            if (parsedInput != null)
            {
                switch (parsedInput.length)
                {
                    case 1:
                        time = timeFormat.parse(parsedInput[0]);
                        System.out.println("Just Time: " + timeFormat.format(time));
                        break;
    
                    case 3:
                    {
                        int duration;
                        Calendar end = Calendar.getInstance();
                        Calendar start = Calendar.getInstance();
                        Calendar timeValue = Calendar.getInstance();
    
                        time = timeFormat.parse(parsedInput[2]);
                        System.out.println("Time: " + timeFormat.format(time));
                        System.out.println("Duration: " + parsedInput[0] + " days.");
    
                        timeValue.setTime(time);
                        transferTime(start, timeValue);
                        transferTime(end, timeValue);
    
                        duration = Integer.parseInt(parsedInput[0]) - 1;
                        end.add(Calendar.DAY_OF_YEAR, duration);
    
                        System.out.println("Start: " + formatCalendar(start));
                        System.out.println("  End: " + formatCalendar(end));
                    }
                        break;
    
                    default:
                        System.out.println("Unrecognized format: " + input);
                        break;
                }
            }
        }
    
        private static void transferTime(
            final Calendar destination,
            final Calendar source)
        {
            destination.set(Calendar.HOUR_OF_DAY, source.get(Calendar.HOUR_OF_DAY));
            destination.set(Calendar.MINUTE, source.get(Calendar.MINUTE));
            destination.set(Calendar.SECOND, source.get(Calendar.SECOND));
        }
    }