seems been sometime away from Java: calendar date difference

I was using this

if( (new Date().getTime() - tradeDate.getTime()) > 1000 * 60 *60 * 24 * 30){
..
}

for making sure trade date not older than 3 months, which then exactly encountered the round off problem mentioned here,
http://tripoverit.blogspot.com/2007/07/java-calculate-difference-between-two.html.

should instead using calendar.before()

    java.util.Date tradeDate = ct.getActionDate();
	Date now = new Date();
	Calendar threeMonBef = Calendar.getInstance();threeMonBef.setTime(now);
	threeMonBef.add(Calendar.MONTH, -3);
	
	Calendar cal1 = Calendar.getInstance();cal1.setTime(tradeDate);

if( cal1.before(threeMonBef)){
..
}

==================================================updated Aug 15, 2012
//only date information should be maintained, instead of datetime, to avoid problem due to timezone difference.

    /**
     * Get previous Business Day of the current system date.
     * Note that this method is heavy because it calls isBusinessDay, which required Database query, multiple time.
     * So, this should be called when it is really required.
     * @param channelCtx
     * @return
     */
    private Date getPreviousBusDay(ChannelContext channelCtx) {


        // yes. I believe that GOCM v1 works only for Tokyo.
        Calendar nowInTokyo = Calendar.getInstance(TimeZone.getTimeZone("Asia/Tokyo"));
        // for testing the situation that Date part is different between Tokyo and GMT.
//        nowInTokyo.set(Calendar.AM_PM, Calendar.AM);
//        nowInTokyo.set(Calendar.HOUR, 7);
//        nowInTokyo.set(Calendar.MINUTE, 30);

        // copy the tokyo date to GMT/PDT calender. truncate datetime part.
        // This is required to get correct result from isBusinessDay method when prev calender date is Holiday.
//        Calendar prevBusDay = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        // weblogic server in US/ewd uses PDT , local dev may use different
        Calendar prevBusDay = Calendar.getInstance(); //use the timeZone of the host
        prevBusDay.set(Calendar.DATE, nowInTokyo.get(Calendar.DATE));
        prevBusDay.set(Calendar.AM_PM, Calendar.AM);
        prevBusDay.set(Calendar.HOUR, 0);
        prevBusDay.set(Calendar.MINUTE, 0);
        prevBusDay.set(Calendar.SECOND, 0);
        prevBusDay.set(Calendar.MILLISECOND, 0);

        prevBusDay.add(Calendar.DATE, -1);
        while(!isBusinessDay(prevBusDay.getTime(), channelCtx)){
            prevBusDay.add(Calendar.DATE, -1);
        }
        return prevBusDay.getTime();
    }
Advertisements

Author: lwpro2

Java J2EE professional

1 thought on “seems been sometime away from Java: calendar date difference”

  1. Look for one that is new with lower mileage on the odometer.
    Banks sometimes have websites where registration
    can happen. The number of management colleges has also increased
    significantly over the past few years.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s