How to Convert Java String to Date

How to Convert Java String to Date thumbnail
Java is a developer language.

The Java language provides developers with a language for application development. Java is used to create desktop applications, website applets and even cellular phone software. A string is a set of characters used for text input. You can convert these string values to a date, provided the string is a valid date entry. This is beneficial when retrieving input from a user that is configured as a string. The date conversion only takes a few lines of Java code.

Instructions

    • 1

      Add the "date formatter" library to your Java code. Libraries are added at the top of the Java code file. Add the code below to your Java source code:

      import java.text.SimpleDateFormat;

    • 2

      Create your two Java variables. Since you are converting a string to a date, the string and date variables are needed. The string holds the first value, and the date variable is used after the conversion. The following code shows you how to create Java variables:

      string strDate;
      SimpleDateFormat format = new SimpleDateFormat();

    • 3

      Assign a string value to your string variable:

      strDate = "2009/08/09"

    • 4

      Convert the string to a date using the "date format" function. The following code converts your date and assigns it to the date variable:

      format = format.parse(strDate);

Related Searches:

References

  • Photo Credit java hot and black image by Pix by Marti from Fotolia.com

Comments

You May Also Like

Related Ads

Featured