-1

I need help in converting a string to a date datatype in Java. I will have to receive a String key in by user and converts to the date datatype.

That means I'm setting my own date and converting to a date datatype.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Heh i have searched in google but i only get the codes which the output is the current time.. So i really have no idea how to do :X – Melissa Feb 11 '15 at 02:31
  • take a look at this. didnt even take me a minute. http://stackoverflow.com/questions/4216745/java-string-to-date-conversion – jmcg Feb 11 '15 at 02:40

1 Answers1

0

First and foremost, define a SimpleDateFormat format and create a date String object that follows the format you predefined. Use SimpleDateFormat.parse(String) to convert a String to java.util.Date object.

More details, please take a look at here.

For complete date and time patterns, please refer to this java.text.SimpleDateFormat JavaDoc.

Wilts C
  • 1,720
  • 1
  • 21
  • 28
  • import java.text.SimpleDateFormat; import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class bla { public static void main(String[] a) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US); Date date = dateFormat.parse(text); } } – Melissa Feb 11 '15 at 02:41
  • Correct. As long as the String 'text' object, as in 'yyyy-MM-dd' format, you would be able to convert it into a Date object. – Wilts C Feb 11 '15 at 03:03