簡體   English   中英

日期/時間轉換/算術

[英]Date/time conversion/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對象中使用它。

我怎樣才能有效地做到這一點? 我應該從日期轉換為日歷,反之亦然? 任何人都可以提供片段嗎?

您應該查看Calendar類。 您可以使用以下構造:

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

它還有一些方法可以檢查你的startDatebefore()新日期before()還是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());

我認為這應該有效...你的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM