Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How can I parse this String into a java.sql.Date format?

 
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");

java.text.ParseException: Unparseable date: "Wed Aug 14 00:00:00 CST 2013"

Also tried SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");

But an Invalid String exception has been thrown.
Thanks
Jack
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z");
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z");



No it doesn't work
parseDateError.png
[Thumbnail for parseDateError.png]
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What value does you pass into the 2nd parameter for the parse() method? And not sure if the locale for the SimpleDateFormat is affecting it, you may want to remove the locale to test.

EDIT forget the 2nd parm in parse().
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jacky Luk wrote:

K. Tsang wrote:SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z");


No it doesn't work


ItDoesntWorkIsUseless (←click).

And I wouldn't post image links. For one thing, some people may not be able to view it (and my old eyes can't read it). Post the code and your error message - exactly as you got it.

Winston
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No, it won't do either. I am getting this error
java.text.ParseException: Unparseable date: "Fri Aug 30 00:00:00 CST 2013"
Thanks
Jack
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guess what the format I gave earlier doesn't work.
new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:Guess what the format I gave earlier doesn't work.
new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");



Java VM is acting really strange at this. I don't know why, by logic, your string should work.
Now even this doesn't
Thanks
Jack
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your code print out the date on line 7? Is tblSalesDetail.getValueAt(i,3) even a Date?
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:Does your code print out the date on line 7? Is tblSalesDetail.getValueAt(i,3) even a Date?



I have set a DefaultTableModel like this

where the forth column is a String (WarrantyExpirationDate)
And yeah, I've checked it, it is a string
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a string alright. The table model can't be wrong right?

The weird thing I don't why this code doesn't work for you.

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:It's a string alright. The table model can't be wrong right?

The weird thing I don't why this code doesn't work for you.



Wait. Hold on a sec, it seems like tblSalesDetail.getValueAt(i,3) was not allowed, probably I was wrong
even though I have checked it using instanceof, it says it found an Object object, it needs to be casted to a string.
But I have done this before. tblSalesDetail.getValueAt(i,3).toString()
frown
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then your table model isn't returning the correct type.

EDIT: if it's Object, what does your instanceof operator say? Is it possible to cast it directly to java.util.Date?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jacky Luk wrote:Java VM is acting really strange at this. I don't know why, by logic, your string should work.


I seem to remember having problems with "MMM" before myself when parsing (but it was with French month abbreviations; possibly because you can't use 3-letter abbreviations with French months).

My advice: Forget about your table model and your GUI entirely, and set up a little test module and test the hell out of SimpleDateFormat. And try lots of different patterns, including maybe just "MMM" and "EEE" on their own, and see what you get.

Winston
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry, Tsang, I've solved it using Joda Time.
Don't spend too much time on it. You've got other important things to do.

Cheers Thanks for helping
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jacky Luk wrote:Cheers Thanks for helping


Jacky,

Please DontWriteLongLines. It makes your thread very hard to read. I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments.

Thanks.

Winston
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Jacky Luk wrote:Cheers Thanks for helping


Jacky,

Please DontWriteLongLines. It makes your thread very hard to read. I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments.

Thanks.

Winston



I am sorry, I will be careful.
thanks
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic