Parsing a string to a date(Creating my own date)
I have a string ie: Mon, 18 Jun 2007 14:39:02 GMT, I would like to parse it and create a date from it, as there is no date.parse(string) method in J2ME, I made my own. The problem is, once I have the integer values parsed from the string, creating a usable date is the hard thing. The problem is, I try to set calendar with the correct values, and when I try to retrive the time as a date, it is todays time rather than the set values. Can any one please help me?
here is my code
public Date stringToDate(String ld){
//ld: Mon, 18 Jun 2007 14:39:02 GMT
Date date =null;
Calendar calendar = Calendar.getInstance();
int index = 0;
String day = ld.substring(index, (index = ld.indexOf(',')));
int dayNo = Integer.parseInt(ld.substring((index+ 2), (index += ld.indexOf(' '))));
String stringMonth = ld.substring((++index), ((index += ld.indexOf(' '))-1));
int intMonth;
if (stringMonth.equalsIgnoreCase("Jan")){
intMonth = 1;
}elseif (stringMonth.equalsIgnoreCase("Feb")){
intMonth = 2;
}elseif (stringMonth.equalsIgnoreCase("Mar")){
intMonth = 3;
}elseif (stringMonth.equalsIgnoreCase("Apr")){
intMonth = 4;
}elseif (stringMonth.equalsIgnoreCase("May")){
intMonth = 5;
}elseif (stringMonth.equalsIgnoreCase("Jun")){
intMonth = 6;
}elseif (stringMonth.equalsIgnoreCase("Jul")){
intMonth = 7;
}elseif (stringMonth.equalsIgnoreCase("Aug")){
intMonth = 8;
}elseif (stringMonth.equalsIgnoreCase("Sep")){
intMonth = 9;
}elseif (stringMonth.equalsIgnoreCase("Oct")){
intMonth = 10;
}elseif (stringMonth.equalsIgnoreCase("Nov")){
intMonth = 11;
}elseif (stringMonth.equalsIgnoreCase("Dec")){
intMonth = 12;
}else{
Calendar c = Calendar.getInstance();
intMonth = c.get(Calendar.MONTH);
}
int year = Integer.parseInt(ld.substring(index, (index += ld.indexOf(' '))));
int hour = Integer.parseInt(ld.substring(++index, (index = ld.indexOf(':'))));
int mins = Integer.parseInt(ld.substring(++index, index = (index +2)));
int secs = Integer.parseInt(ld.substring(++index, index = (index +2)));
calendar.set(Calendar.DAY_OF_MONTH, dayNo);
calendar.set(Calendar.MONTH, intMonth);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, mins);
calendar.set(Calendar.SECOND, secs);
//PROBLEM with what this returns!
date = calendar.getTime();
return date;
}

