2

I was trying to format a string into date.

For this I have written a code:-

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdf.format( cal.getTime() ));

This is fine.. But now I want to convert a string into a date formatted like above.. For example

String dt="2010-10-22";

And the output should be like this:- 2010-10-22T00:00:00

How do I do this?

Kuntal Basu
  • 830
  • 2
  • 12
  • 28
  • 1
    Have you tried looking at other answers in stackoverflow? e.g. http://stackoverflow.com/questions/3487898/how-to-parse-follow-date-in-java – John Pickup Nov 01 '10 at 08:40

5 Answers5

5
String dt = "2010-10-22";

SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition ps = new ParsePosition(0)
Date date = sdfIn.parse(dt, pos)

SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

System.out.println(sdfOut.format( date ));
David Barr
  • 189
  • 4
  • 1
    You don't need the `new ParsePosition(0)`. Just call `sdfIn.parse(dt)`, it defaults to the first position. – Steve Kuo Nov 01 '10 at 14:49
2

This should do it for you, remember to wrap it in a try-catch block just in case.

DateFormat dt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
try
{
Date today = dt.parse("2010-10-22T00:00:00");                       
System.out.println("Your Date = " + dt.format(today));       
} catch (ParseException e)    
{
//This parse operation may not be successful, in which case you should handle the ParseException that gets thrown.
//Black Magic Goes Here
} 
JonVD
  • 4,258
  • 24
  • 24
  • "Just in case" what? I dislike code examples that "handle" exceptions by printing the stacktrace, as they appear to function correctly but completely elide a very important part of the solution. It's better not to write the catch block at all, at least then the compiler forces the user to **think** about what to do with the exception. To that end I'd prefer that your answer mentions that the ParseException can be thrown, and should be handled *appropriately*. – Andrzej Doyle Nov 01 '10 at 08:47
  • Thanks for the answer but I think David's answer is more better – Kuntal Basu Nov 01 '10 at 08:50
  • I'm glad you found your answer! I don't see anything inherently dangerous in printing a stack trace in a snippet. God have mercy on any developer that copies code directly from the internet and deploys it verbatim. The trace is there so that the developer does stop and think, but also has a starting point to begin figuring out the problem. If you need the complier to force the developer to look at his code then something is seriously wrong. Just my opinion of course! – JonVD Nov 01 '10 at 08:56
  • Actually I am a beginner in programing world so don't have so much knowledge. What looks easy and safe I used it. If your logic is more perfect then please explain about it. So that I could learn something. Thanks.. – Kuntal Basu Nov 03 '10 at 13:55
1

If your input is going to be ISO, you could also look at using the Joda Time API, like so:

LocalDateTime localDateTime = new LocalDateTime("2010-10-22");
System.out.println("Formatted time: " + localDateTime.toString());
puug
  • 941
  • 6
  • 5
0

Date Format Example

Containing the Conversion of String Date object from one format to another

Community
  • 1
  • 1
ThmHarsh
  • 601
  • 7
  • 7
0

The same class you use for output formatting of dates can also be used to parse dates on input.

To use your example, to parse the sample date:

String dt = "2010-10-22";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormatter.parse(dt));

The fields that are not specified (ie. hour, minutes, etc) will be 0. So your same code can be used to format the date on output.

gavinb
  • 19,278
  • 3
  • 45
  • 60