Results 1 to 6 of 6
Thread: Convert String to Calendar
- 08-06-2010, 03:42 AM #1
Convert String to Calendar
I'm trying to convert a String to Calendar. This is my attempt:
I end up getting this exception: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.
The only 'T' in the String content is T in Thu. Consequently, I decided to do this: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)
and evidently enough, I get an exception:Java Code:SimpleDateFormat format1 = new SimpleDateFormat("EEE"); format1.applyPattern("Wed"); //line 63
Now when I do something like this: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)
I get no exception.Java Code:SimpleDateFormat format1 = new SimpleDateFormat("mm/dd/yy"); format1.applyPattern("05/18/92");
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)
-
Look at the SimpleDateFormat class API. Are you sure you want to call toPattern(...) much less with a parameter?
- 08-06-2010, 03:51 AM #3
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)
- 08-06-2010, 05:17 AM #4
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:
(Source: http://www.epochconverter.com/)Java Code:long epoch = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse("01/01/1970 01:00:00");Last edited by Zack; 08-06-2010 at 05:19 AM.
- 08-06-2010, 09:36 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
parse() returns a Date, not a long...
But yes, the OP should be using parse().
- 08-06-2010, 03:02 PM #6
Similar Threads
-
convert obj to string
By isme in forum New To JavaReplies: 11Last Post: 06-14-2010, 10:54 AM -
How to convert a String into an Hexadecimal ?
By ze snow in forum New To JavaReplies: 7Last Post: 02-16-2010, 10:31 PM -
how to convert a string to an integer
By LAW in forum New To JavaReplies: 6Last Post: 11-09-2009, 03:29 AM -
Convert Char To String
By fh84 in forum New To JavaReplies: 15Last Post: 10-28-2009, 09:59 PM -
convert string to a double?
By javaMike in forum Advanced JavaReplies: 2Last Post: 11-27-2007, 03:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks