Results 1 to 6 of 6
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default Convert String to Calendar

    I'm trying to convert a String to Calendar. This is my attempt:

    Java Code:
    String content = "Thu Aug 05 21:07:55 CDT 2010";
    SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    format.applyPattern(content); //line 65
    ...
    //my attempt stopped here because of Exception.
    I end up getting this exception:
    Java Code:
    java.lang.IllegalArgumentException: Illegal pattern  character 'T'
    	at java.text.SimpleDateFormat.translatePattern(Unknown Source)
    	at java.text.SimpleDateFormat.applyLocalizedPattern(Unknown Source)
    	at sudoku.SudokuWorld.getTimeOfLastExecution(SudokuWorld.java:65)
    	at sudoku.SudokuWorld.<init>(SudokuWorld.java:28)
    	at sudoku.DriverProgram.main(DriverProgram.java:5)
    The only 'T' in the String content is T in Thu. Consequently, I decided to do this:

    Java Code:
    SimpleDateFormat format1 = new SimpleDateFormat("EEE");
    format1.applyPattern("Wed"); //line 63
    and evidently enough, I get an exception:

    Java Code:
    java.lang.IllegalArgumentException: Illegal pattern character 'e'
    	at java.text.SimpleDateFormat.compile(Unknown Source)
    	at java.text.SimpleDateFormat.applyPattern(Unknown Source)
    	at sudoku.SudokuWorld.getTimeOfLastExecution(SudokuWorld.java:63)
    	at sudoku.SudokuWorld.<init>(SudokuWorld.java:28)
    	at sudoku.DriverProgram.main(DriverProgram.java:5)
    Now when I do something like this:
    Java Code:
    SimpleDateFormat format1 = new SimpleDateFormat("mm/dd/yy");
    format1.applyPattern("05/18/92");
    I get no exception.

    Anyone know what I'm doing wrong? Thanks in advance!
    Last edited by Lil_Aziz1; 08-06-2010 at 03:54 AM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

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

    Default

    Look at the SimpleDateFormat class API. Are you sure you want to call toPattern(...) much less with a parameter?

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Oh sorry. In my IDE it is applyPattern(). I manually typed it in here and did it incorrectly.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

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

    Default

    applyPattern is not used for this. Take a look at the documentation (SimpleDateFormat (Java 2 Platform SE v1.4.2)); it's used to convert a string like hh:mm:ss to 13:26:59.

    EDIT: You're looking for something more like this:
    Java Code:
    long epoch = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse("01/01/1970 01:00:00");
    (Source: http://www.epochconverter.com/)
    Last edited by Zack; 08-06-2010 at 05:19 AM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    parse() returns a Date, not a long...
    But yes, the OP should be using parse().

  6. #6
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Oh dur.. applyPattern is to change this pattern. Thanks for the help!
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Similar Threads

  1. convert obj to string
    By isme in forum New To Java
    Replies: 11
    Last Post: 06-14-2010, 10:54 AM
  2. How to convert a String into an Hexadecimal ?
    By ze snow in forum New To Java
    Replies: 7
    Last Post: 02-16-2010, 10:31 PM
  3. how to convert a string to an integer
    By LAW in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 03:29 AM
  4. Convert Char To String
    By fh84 in forum New To Java
    Replies: 15
    Last Post: 10-28-2009, 09:59 PM
  5. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM

Posting Permissions

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