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;

}

[4963 byte] By [-fluffy-a] at [2007-11-27 8:16:29]
# 1

use this to split the date...

public String[] split(String str, String ch){

java.util.Vector v=new java.util.Vector();

while(str.indexOf(ch) != -1){

String tmp=str.substring(0, str.indexOf(ch)).trim();

if(tmp.length()>0)v.addElement(tmp);

str=str.substring(str.indexOf(ch)+1,str.length());

}

String[] returned=new String[v.size()];

for (int i=0;i<v.size();i ++) {

returned[i]=(String)v.elementAt(i);

}

return returned;

}

and create your Date with the fields of the array>

suparenoa at 2007-7-12 20:01:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2
Do not wary. Your code will work in the real device. Emulators often do not give satisfied result while dealing with Date and Calenders.
find_suvro@SDNa at 2007-7-12 20:01:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
Which WTK are you using? Try a newer one if possible or try in another emu.Try one at: http://forum.nokia.com
jonathan.lea at 2007-7-12 20:01:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4
Thanks for your help, it worked on the device no problem, stupid emulators :D
-fluffy-a at 2007-7-12 20:01:23 > top of Java-index,Java Mobility Forums,Java ME Technologies...