How to convert String to Date

Joined
Sep 6, 2008
Messages
1
Reaction score
0
The codes below show you how to convert String to Date in Java.

The copyright of the codes belongs to javaeecoding.com

HTML:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date; 

public class StringToDateConverter 
{    
    public static void main(String[] args)    
    {        
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");         
        
       try        
        {            
             // to convert String to Date in format yyyy-MM-dd            
             Date date1 = dateFormat1.parse("2008-08-09");
             System.out.println("date1 = " + date1);        
        }         
        catch (ParseException ex)        
        {
              ex.printStackTrace();        
         }    
    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top