Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA
Accepted Solution

String Date Between Dates

Hello.  I've come up with a method that seems to work fine.  Just thought there might be a more effective way of determining if a string date (must be from a string) is between 2 other dates (from string).

 

TimeZone clientZone = TimeZone.getDefault();

		String dateToCompare="2012-10-03";
		String dateBegin="2012-10-01";
		String dateEnd="2012-10-07";
		
		Date date1= new Date(HttpDateParser.parse(dateToCompare));
		Date date2= new Date(HttpDateParser.parse(dateBegin));
		Date date3= new Date(HttpDateParser.parse(dateEnd));
		
		Calendar calToCompare = Calendar.getInstance(clientZone);
		Calendar calBegin = Calendar.getInstance(clientZone);
		Calendar calEnd = Calendar.getInstance(clientZone);
		
		calToCompare.setTime(date1);
		calBegin.setTime(date2);
		calEnd.setTime(date3);
		
		if(calToCompare.after(calBegin) && calToCompare.before(calEnd)) {
			Dialog.alert("date is between");
		}
		else {
			Dialog.alert("date is NOT between");
		}
		

 

Is there a more effective way to accomplish this?  Thank you.

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: String Date Between Dates

Date date1= new Date(HttpDateParser.parse(dateToCompare));
Date date2= new Date(HttpDateParser.parse(dateBegin));
Date date3= new Date(HttpDateParser.parse(dateEnd));

 

long compareDateLong = date1.getTime();

long beginDateLong = date2.getTime();

long endTimeLong = date3.getTime();

 

if ( beginDateLong < compareDateLong ) && ( endTimeLong > compareDateLong ) {

Dialog.alert("date is between");
}
else {
Dialog.alert("date is NOT between");
}

Please use plain text.
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: String Date Between Dates

Thank you, Peter.

Please use plain text.
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: String Date Between Dates

Wondering if you could offer me some assistance or pointers.  When I use the following code

 

strTheDate = new Date(HttpDateParser.parse("2012-11-09"));
Dialog.alert(strTheDate.toString());

 

The dialog box displays the 8th of Nov, not the 9th.

 

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: String Date Between Dates

I suspect a Time Zone issue, but I don't use HttpDateparser so have never looked at how it processes when there is not a Time Zone specified.  Review that and/or try with a Time Zone specified to see if you see some effect. 

Please use plain text.
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: String Date Between Dates

Thanks.  I did try with a time and time zone but still same issue.

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: String Date Between Dates

From the testing I have just done, the string:

2012-11-09

is treated midnight on the date specified, if you were on the GMT line.  But when you do this:

strTheDate.toString()

the time is converted to local time.  I suspect this explains the difference. 

 

Check the time zone specified on the device/simulator that you are using. 

Please use plain text.
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: String Date Between Dates

Thanks time zone on device/simulator is -4 (Santiago).  Do i need to somehow specify this when parsing?

Please use plain text.
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: String Date Between Dates

Seems that when I specify date as "2012-11-09T00:00-04:00" that I get the correct date.  Just need to determine now how to get string value of -04:00 from device.

Please use plain text.
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: String Date Between Dates

Ok.  I can get the raw offset using

 

TimeZone clientZone = TimeZone.getDefault();
Calendar cal = Calendar.getInstance(clientZone);
Dialog.alert(Integer.toString(clientZone.getRawOffset() / 3600000));

 

Which will return -5.  I need to somehow now format that as hour.  eg.  -05:00

 

Is there a way to properly do this?

Please use plain text.