Platforms: J2ME CLDC 1.1 MIDP 2.1
BouncyCastle LightweightCryptoAPI for J2ME (www.bouncycastle.org)
The code fragment here is to convert an instance of org.bouncycastle.asn1.x509.Time into a J2ME java.util.Date instance that contains the current time according to the timezone and time specified as per UTC. This code can be used for example, to get a Date instance of TBSCertificateStructure.getStartDate() or getEndDate(). The Date objects can than be used to check if the certificate has not expired.
One requirement would be to get the current timezone of the user somehow. Right now, I have hardcoded it as "-08:30" for PST. For an Indian timezone, it must be "+05:30" and so on.
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.bouncycastle.asn1.x509.Time;
/**
*
* @author Himanshu Ranavat
*/
public class BouncyCastleDateUtil {
/** Creates a new instance of BouncyCastleDateUtil */
public BouncyCastleDateUtil() {
}
public Date convertTime2Date(Time time) {
//get string representation of Time Object. will be usually as per GMT+00:00
String date = time.getTime();
//eg: time = 2008 0207 2300 00 GMT + 00:00
int year = getIntValueForString(date.substring(0, 4)) ;
int month = getIntValueForString(date.substring(4,6));
int day = getIntValueForString(date.substring(6,8));
int hour = getIntValueForString(date.substring(8,10));
int minutes = getIntValueForString(date.substring(10,12));
int seconds = getIntValueForString(date.substring(12,14));
String meanTime = date.substring(14,17);
//check if mean time is UTC or GMT is supported
meanTime = getActualMeanTime(meanTime);
String timeZoneDetails = date.substring(17);
long timeZoneHr = getLongValueForString(timeZoneDetails.substring(1,3)) * 60 * 60 * 1000;
long timeZoneMin = getLongValueForString(timeZoneDetails.substring(4,6)) * 60 * 1000;
//get current time zone offset in milliseconds as +ve or -ve
long timeZoneOffset = 0;
if ( timeZoneDetails.charAt(0) == '-' ) { //adjust time as per GMT
timeZoneOffset = timeZoneHr + timeZoneMin;
} else {
timeZoneOffset = -(timeZoneHr + timeZoneMin);
}
//set the calendar object with the parameters passed in from Time object
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone(meanTime));
cal.set(Calendar.YEAR, year);
//reduce month by 1 since 0 indicates January
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minutes);
cal.set(Calendar.SECOND, seconds);
//date in Calendar instance is as per GMT or UTC. we need to add the time zone offset
//obtained from the Time object
return new Date(cal.getTime().getTime() + timeZoneOffset);
}
private int getIntValueForString (String p_value){
return Integer.valueOf(p_value).intValue();
}
private long getLongValueForString (String p_value){
return Integer.valueOf(p_value).longValue();
}
private String getActualMeanTime(String p_meanTime){
String[] zones = TimeZone.getAvailableIDs();
for (int i = 0 ; i < defaulttimezone = "GMT">
Sample Output:
Thu Feb 07 23:49:00 UTC 2008 - for a Time instance whose getTime method returns "20080207234900GMT+00:00"
Thu Feb 07 23:49:43 UTC 2008 - Date representation of the same time as in example
The precision is not to the seconds as shown by the sample output but I think it should be sufficient. It servers my purpose and hope it helps you'll too. Please leave me comments or suggestions to improve it.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment