Home Java Java-conversion Java String To Date
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java String To Date

Advertisement
Java string to date example explains you how to change a string into a date.

Java String To Date

In this section you will read about how to convert a string to date. Here you will read how the specified string into date format can be convert into java.util.Date. Java string to date convert explains the conversion of Java string written into the specified format to a Java date object. Java provides two types of Date class from different packages. These are java.util.Date and java.sql.Date. Both of these classes are used for creating Date object in which java.sql.Date class is used for working with date that is either fetched from or insert into the database and the java.util.Date class is used to date object to manipulate with the Java object.

Followings are the two Date classes using which we can manipulate with these objects in the Java classes at the time of development :

  • java.util.Date : This class has various constructors using which you can create the multiple date objects. These are specified as follows :
    • Date() : This constructor creates a date object, measured in millisecond.
       
    • Date(long date) : This constructor creates a date object with the specified date given as milliseconds, calculated as the "epoch" standard base time i.e. calculated since 1.1.1970, 00:00:00 GMT.
       
  • java.sql.Date : This class allows for creating date objects using its constructors, measured in milliseconds. This date object is wrapped by a thin wrapper seeing which JDBC understands that this DATE value is an SQL date value.
    • Date(long date) : This constructor creates a date object with the specified date given as milliseconds, calculated as the "epoch" standard base time i.e. calculated since 1.1.1970, 00:00:00 GMT.

Example

Here I am giving a simple example which will demonstrate you how a date string will be convert into Java date object. In this example we will first specify a date string then we will create an object of SimpleDateFormat that is used to specify a date format. Then I have used the parse() method of SimpleDateFormat that parse the String into Date object and returns a Date. Then I have used the System.out.println() to show the output on console.

StringToDate.java

import java.util.*;
import java.text.*;

public class StringToDate {

	public static void main(String args[])
	{
		String string = "21 JUN 2013";
		SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");		
		try {
			Date date = sdf.parse(string);
			System.out.println(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}		
	}
}

Output :

When you will compile and execute the above example you will get the output as follows :

Download Source Code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 21, 2013

Ask Questions?    Discuss: Java String To Date  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com