1

Using J2ME, netbeans 7.2, Developing a mobile app..

I have converted the Datefield value to a String and Now want to put it back to a Datefield. To do this I need to convert the String back to Datefield, I am using the following code but its not happening.

long myFileTime = dateField.getDate().getTime(); // getting current/set date from the datefield into long
         String date = String.valueOf(myFileTime); // converting it to a String to put it back into a different datefield

         Date updatedate= stringToDate(date); // passing the string 'date' to the Method stringToDate() to convert it back to date.
                 dateField1.setDate(updatedate); // updating the date into the new datefield1

public Date stringToDate(String s) 
{
    Calendar c = Calendar.getInstance();

    c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
    c.set(Calendar.MONTH, Integer.parseInt(s.substring(3, 5)) - 1);
    c.set(Calendar.YEAR, Integer.parseInt(s.substring(6, 10)));

    return c.getTime();
}
gnat
  • 6,213
  • 108
  • 53
  • 73
Ramesh Sivaraman
  • 1,295
  • 3
  • 19
  • 32
  • Could you please specify what is the format of the myFileTime variable? Is it 25122012 for 25 DEC 2012 ? – nikkatsa Nov 22 '12 at 08:45
  • it is in this format '25122012' plus the time. – Ramesh Sivaraman Nov 22 '12 at 08:53
  • I think that the reason your code is not functioning well, is that because the `long myFileTime` is not in the format you are thinking. When you do `Date.getTime()` it returns you the miliseconds after January 1, 1970 00:00:00 GMT and not a long in the format of 25122012. In that case the below code sample will help you. – nikkatsa Nov 22 '12 at 09:01

1 Answers1

1

Since you have mentioned that you have the long myFileTime around, you should be able to use:

Date updatedate=new Date(myFileTime);

To convert back to your date. If only your String is available, you should modify your function to this:

public Date stringToDate(String s){
  Calendar c = Calendar.getInstance();

  c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
  c.set(Calendar.MONTH, Integer.parseInt(s.substring(2, 4))-1 );
  c.set(Calendar.YEAR, Integer.parseInt(s.substring(4, 8)));

  return c.getTime();
}

Note the changed indexes.

In Java SE, you should be able to use the following line, instead of setting each fields separately:

c.setTimeInMillis(Long.parseLong(s));

Since in s you have the dateField.getDate().getTime() that is equal to myFileTime, the number of seconds starting with January 1, 1970, based on your provided code.

Your stringToDate should work only if your string will have the following format: ddMMyyyy. Also note that in this case you should use a SimpleDateFormat to parse, something like:

Date updatedate = new java.text.SimpleDateFormat("ddMMyyyy HH:mm:ss").parse(date);
dan
  • 13,132
  • 3
  • 38
  • 49
  • I understood, I have time as well. So you meant to use only the above line instead of all the set items in stringToDate() method ? – Ramesh Sivaraman Nov 22 '12 at 08:56
  • I am not able to get SimpleDateFormat, unable to add library for that. Using J2ME so... – Ramesh Sivaraman Nov 22 '12 at 08:58
  • @raam030 You can do either, see my updated answer. If you have the `myFileTime` around you should use `c.setTimeInMillis`. – dan Nov 22 '12 at 09:00
  • I am getting an error when used c.setTimeInMillis, says 'no suitable method found for 'parseLong(long)'... – Ramesh Sivaraman Nov 22 '12 at 09:13
  • @raam030 `Long.parseLong(s)` needs a `String` not a `long`. If you already have the `long` you can use directly: `c.setTimeInMillis(your_long)` or even simpler `new Date(your_long)` – dan Nov 22 '12 at 09:23
  • -1 in java ME MIDP/CLDC, [Calendar.setTimeInMillis](http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/java/util/Calendar.html#setTimeInMillis%28long%29) is `protected`, as opposed to public in Java SE. Also, there's no SimpleDateFormat there - [no `java.text` package at all](http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/overview-summary.html) – gnat Nov 22 '12 at 09:27
  • 1
    @raam030 See my updated answer, some of your initial indexes were drifted a bit. – dan Nov 22 '12 at 09:40
  • 1
    its taking my long and updating the datefield correctly. Thanks for the help Dan. Such a simple thing I was thinking too much !!!!....Thank you all... long myFileTime = dateField.getDate().getTime(); date = String.valueOf(myFileTime);Date d = new Date(myFileTime); dateField1.setDate(d); – Ramesh Sivaraman Nov 22 '12 at 10:34