×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    10

    Convert String to Date

    Hi all,

    i am using a J2ME appliation. i am able to convert date to String using Calendar. but i am not able to convert date to String in another requirement. please help me.

    thanks in advance.

    regards,
    Bapitha .N.C

  2. #2
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: Convert String to Date

    Hi,

    I'm sorry, your request is a bit confusing. The topic title says "Convert String to Date", but in your post you say
    Quote Originally Posted by bapithanc View Post
    but i am not able to convert date to String in another requirement
    So, do you want to convert a String to a Date, or the other way around? And what is that other requirement?

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    10

    Re: Convert String to Date

    Hi

    Now i need to convert String to Date.

    regards,

  4. #4
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: Convert String to Date

    This tutorial explains how to do that.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    10

    Re: Convert String to Date

    Hi

    Thankx for the immediate reply. the problem is i am unable to import Java.text.*; and so i am unable to use SimpleDateFormat. i already tried this. please help me if their is any other alternative or should i use any other platform.

    Note:
    Netbeans IDE 6.0.1 is the IDE USed.

    regards,

  6. #6
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: Convert String to Date

    Hi bapithanc,

    here's a simple method that will convert a String in "dd-mm-yyyy" format into a Date object:

    Code:
    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();
    }
    Note that it does not check for any Exceptions, so you should care about them too.
    Also, you can easily adapt the code above to accept other date formats, just moving the substring indexes.

    Hope it helps,
    Pit

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    10

    Re: Convert String to Date

    Hi jappit,

    Thankx a lot. it helped me. i am really thank full to Nokia for providing too good Discussion Boards.

    thankx again

Similar Threads

  1. Get the date as String from j2me form
    By bvsbh in forum Mobile Java General
    Replies: 5
    Last Post: 2012-05-05, 18:44
  2. convert date value into string and vice versa
    By mobile_don in forum Mobile Java General
    Replies: 1
    Last Post: 2007-05-10, 12:07
  3. Convert bytes in String
    By decisor in forum Mobile Java General
    Replies: 8
    Last Post: 2006-08-03, 18:56
  4. convert Date object to string
    By artemg in forum Mobile Java General
    Replies: 4
    Last Post: 2006-03-03, 04:56
  5. Personal
    By JSmith77 in forum PersonalJava
    Replies: 4
    Last Post: 2003-07-30, 12:52

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×