1

I have a website which supplies date in 2 formats: 28th June 2009 or June 2009.

Now I would like to convert both of these into the same format yyyy-mm-dd hh:mm:ss using MySQL and Java.

SimpleDateFormat gives an error: "Unparsable Date". What's the solution?

1 Answer 1

4

What about June 2009 as you can not say its a date you need to make it a date by adding a day in this month-year format. Ex.. add first day of month here and make it 1 June 2009 then parse it in desired format.

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

       public class Test {

          public static void main(String[] args) throws IOException, ParseException 
          { 
              String dateStr = "28 June 2009";
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
              System.out.println(sdf.format(new Date(dateStr)));
          }
   }
Sign up to request clarification or add additional context in comments.

5 Comments

new Date(url) <--- What is the 'url' ?
june 2009 is ok... but what about 28th june 2009? how does SimpleDateFormat detect the 'th' after the day? if it way simply '28 June 2009' i would have used the Parse method in SimpleDateFormat...
and isn't Date(str) a deprecated method now?
yes Date(str) is deprecated you can use Calendar for that. And 28th is and string just remove th from it and move further.
yep...that's what i'm doing...was just looking for a more efficient way using just the SimpleDateFormat and no String manipulations...anyways thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.