0

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 SimpleDateFormatter("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"

karex
  • 221
  • 1
  • 6
  • 14

4 Answers4

3

You can't add two Dates directly using the + operator. You can, however, add a certain number of hours or minutes, or add their timestamps.

However, there seems to be a general consensus that the JDK Time API is broken. They recommend using Joda Time, which provides additional features, such as adding a Period or Duration to a DateTime:

DateTime dt = new DateTime(2005, 3, 26, 12, 0, 0, 0);
DateTime plusPeriod = dt.plus(Period.days(1));
DateTime plusDuration = dt.plus(new Duration(24L*60L*60L*1000L));

For more information see http://joda-time.sourceforge.net/quickstart.html

Community
  • 1
  • 1
Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
1

You'll want to try something like this:

Date now = new Date();
Date delay = new SimpleDateFormat("HH:mm:ss").parse("02:15:30");
Date after = new Date(now.getTime() + delay.getTime());
Scott
  • 16,711
  • 14
  • 75
  • 120
1

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));
    }
}
DwB
  • 37,124
  • 11
  • 56
  • 82
  • can you explain me why you have used transferTime instead of just call 2 more times setTime(time). is it more efficient or there is other reason? – karex Jun 14 '13 at 20:04
0

Dates use longs to represent time internally, try the following:

Date after = new Date(now.getTime() + delay.getTime());
Robert Mitchell
  • 892
  • 7
  • 9