日期/时间转换/算术

时间:2012-07-31 01:14:01

标签: java date java.util.date date-conversion date-arithmetic

我在Java 1.7下面的代码:

DateFormat df = DateFormat.getInstance();
Date startDate = df.parse("07/28/12 01:00 AM, PST");

上述日期时间(07/28/12 01:00 AM,PST)位于可配置的属性文件中。如果这个日期时间已经过去,那么我需要获取当前日期,从当前日期的太平洋标准时间上午01:00以上的字符串设置时间部分。如果这个时间也已经过去了,那么第二天就可以了。从上面的字符串中设置时间部分。最终的对象应该是Date,因为我需要在Timer对象中使用它。

我该如何有效地做到这一点?我应该从日期转换为日历,反之亦然?任何人都可以提供片段吗?

2 个答案:

答案 0 :(得分:2)

您应该查看Calendar课程。您可以使用以下结构:

    Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);
    cal.add(Calendar.DAY_OF_YEAR, 1);

它还有一些方法可以检查您的startDatebefore()还是after()新日期(使用当前时间)。

在编写内置的Java日期/时间结构时,如果我没有插入Joda Time,那么我将会失职,被许多人认为优于本机Java实现。

编辑:

显示Date.compareTo()进程的示例会更有效,因为Calendar.before()Calendar.after()需要与其他Calendar对象进行比较,这对于创建。

看看以下内容:

    DateFormat df = DateFormat.getInstance();
    Date startDate = df.parse("07/28/12 01:00 AM, PST");
    Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);
    Date now = new Date();
    if (startDate.compareTo(now)< 0) {
        System.out.println("start date: " + startDate + " is before " + now);

        Calendar nowCal = Calendar.getInstance();
        nowCal.add(Calendar.DAY_OF_YEAR,1);
        cal.set(Calendar.DAY_OF_YEAR, nowCal.get(Calendar.DAY_OF_YEAR));

    } else  if (startDate.compareTo(now) == 0) {
        System.out.println("startDate: " +startDate + " is equal to " + now);
    } else {
        System.out.println("startDate: " + cal + " is after " + now);

    }
    System.out.println(cal.getTime());

答案 1 :(得分:0)

我认为这应该有用......你的algorthim让我的头旋转了一点,我在不同的时区,所以原来的字符串不起作用:P

    try {

        DateFormat df = DateFormat.getInstance();
        Date startDate = df.parse("28/07/2012 01:00 AM");

        System.out.println("StartDate = " + startDate);

        Date callDate = startDate;

        Calendar today = Calendar.getInstance();
        Calendar start = Calendar.getInstance();
        start.setTime(startDate);

        System.out.println("Today = " + today.getTime());

        // If this date time is already passed
        // Tue Jul 31 12:18:09 EST 2012 is after Sat Jul 28 01:00:00 EST 2012
        if (today.after(start)) {

            //then I need to get the current date, set the time part from above string in the current date
            Calendar timeMatch = Calendar.getInstance();
            timeMatch.setTime(startDate);
            timeMatch.set(Calendar.DATE, today.get(Calendar.DATE));
            timeMatch.set(Calendar.MONTH, today.get(Calendar.MONTH));
            timeMatch.set(Calendar.YEAR, today.get(Calendar.YEAR));

            //& if this time is also already passed, then get the next day & set the time part from above string in it
            if (timeMatch.after(today)) {

                timeMatch.add(Calendar.DATE, 1);

            }

            callDate = timeMatch.getTime();

        }

        System.out.println("CallDate = " + callDate);

    } catch (ParseException exp) {

        exp.printStackTrace();

    }

这会产生以下输出

StartDate = Sat Jul 28 01:00:00 EST 2012
Today = Tue Jul 31 12:18:09 EST 2012
CallDate = Tue Jul 31 01:00:00 EST 2012