Java比较日期

时间:2013-06-14 16:46:10

标签: java

我有一个字符串,通常格式为“HH:mm:ss”,但也可以显示为“x days HH:mm:ss”。我必须将它解析为Date对象,我想从字符串计算此时间之后的日期。怎么办呢?

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

但遗憾的是它不起作用。它增加了但作为“延迟”的非常小的价值

4 个答案:

答案 0 :(得分:3)

您无法使用Date运算符直接添加两个+。但是,您可以添加一定的小时数或分钟数,或者添加时间戳。

然而,there seems to be a general consensus that the JDK Time API is broken。他们建议使用Joda Time,它提供了其他功能,例如向Period添加DurationDateTime

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));

有关详细信息,请参阅http://joda-time.sourceforge.net/quickstart.html

答案 1 :(得分:1)

你会想尝试这样的事情:

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

答案 2 :(得分:1)

亮点:

  • 使用String.split确定您是否有时间,持续时间和时间。
  • 假设日期持续时间从当前日期开始。
  • 持续时间将是当前日期+(持续时间-1)更多天(示例不验证持续时间)。
  • 使用Calendar作为日期内容。
  • Calendar.add(Calendar.DAY_OF_YEAR, xxx)给出了结束日期。

尝试这样的事情:

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));
    }
}

答案 3 :(得分:0)

日期使用long来表示内部时间,请尝试以下操作:

Date after = new Date(now.getTime() + delay.getTime());