Results 1 to 5 of 5
  1. #1
    cowboy2010 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default how we can convert string to date?

    Hi

    I wanna read a date that is stored as String , parse it to date and add some days to that and do some other calculation base on date, how can I do that ?

    Sincerely
    Cowboy

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your best bet is to look up the SimpleDateFormat class in the Java API. It will likely have all that you need to parse a String into a Date or to format a Date object into a String.

    Luck!

  3. #3
    cowboy2010 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your best bet is to look up the SimpleDateFormat class in the Java API. It will likely have all that you need to parse a String into a Date or to format a Date object into a String.

    Luck!
    thanks for your help would you please show me an example that how can I convert a string to a date and then add 30 days to that?
    I tried many of those class and methods but still i cannot use them properly
    the string that I have is in this format for example : 20090319

    regards
    Cowboy

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default

    Hi,

    Can you post your code that you've created so far. Let's see if I can help you solving your problem.

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You first need to identify which format your date is in, using the chart on the page Fubarable provided.

    * Firstly, you have four numbers for the year (2009); that would be represented as four of the letter y.
    * Next, you have two digits for the month. You can see at the top of that chart that M is the symbol for month, and that repeating it twice will give you a two-digit month.
    * Finally, you have the day, which is the "day in month" entry of that chart. Again, you have two, so you will have "dd" as the symbol for that part.

    Hence, your date string looks like this: "yyyyMMdd".

    Now, when a SimpleDateFormat is created, it is usually created from a string format like the one we've created above. So we call this constructor, with "yyyyMMdd" as a parameter.

    Next, we need to take your string "20090319" and parse it with the SimpleDateFormat. Simply enough, you can call .parse("20090319") on your object, and it will return a Date object.

    With this Date object, you can now add to it using the Calendar object, as described here.

Similar Threads

  1. Convert date from database
    By karq in forum New To Java
    Replies: 5
    Last Post: 07-13-2010, 08:44 AM
  2. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  3. how to convert date format
    By saran123 in forum New To Java
    Replies: 5
    Last Post: 10-16-2008, 06:10 PM
  4. String to Date conversion
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 07:16 PM
  5. Date as String to TimeStamp
    By ironballs in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 01:43 PM

Posting Permissions

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