1

I am very new to java and I want user to input time and date on which a football match needs to be scheduled. I'm using SWING jForm as the interface. After getting that date I also want to put that in the database. After searching a lot either I couldn't search the right code or I can't understand that. I'm working on a pretty long project and left with no time to watch tutorial. Any help in this regard would be very highly appreciated.

Or just tell me if I have "YYYY/MM/DD HH:MM:ss:SSS" in a String then how can I convert that to Date format?

Date d = (Date)dateString didn't worked. 

Note I am using drag and drop to create items on jFrame.

Haseeb Jadoon
  • 450
  • 6
  • 20
  • you may use tokenizer StringTokenizer date= new StringTokenizer(dateString, "/ : ", true); – iShaalan Nov 23 '13 at 16:58
  • A quick [Google search](https://www.google.com/#q=java+parse+date) brought up [more](http://stackoverflow.com/questions/999172/how-to-parse-a-date) [than](http://stackoverflow.com/questions/4496359/how-to-parse-date-string-to-date) [one](http://stackoverflow.com/questions/16336643/date-time-parse-in-java) StackOverflow-Answer regarding this topic, one of it being a more or less exact duplicate. – Pit Nov 23 '13 at 16:59

3 Answers3

1

You may try like this:

SimpleDateFormat str =new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");

like this:

public static void main(String[] args) throws Exception {
    String str = "Sat Nov 23 20:29:30 JST 2013";
    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
    Date d =  df.parse(str);  
    System.out.println(d);
}

Also check SimpleDateFormat

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

(a) Doing date-time work at the last minute is a bad idea. Date-time can be a surprisingly thorny topic.

(b) See my answer to a similar question.

My example code there uses the third-party library Joda-Time, a popular replacement for the notoriously bad java.util.Date/Calendar classes bundled with Java.

That code is just what you need, though you'll need to:

  • Adjust the format to your particular needs
  • Adjust the time zones to whatever you are handling

Do most of your date-time work within the Joda-Time framework. At the beginning and end, convert between Joda-Time DateTime objects and java.util.Date objects. Lots of Q&A here on StackOverflow to show you that conversion.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Use Date.parse(String), see javadoc for more details.

Blub
  • 3,762
  • 1
  • 13
  • 24