convert date string to date in java code example

Example 1: string to date conversion java

import java.text.SimpleDateFormat;  
import java.util.Date;  
public class StringToDateExample1 {  
public static void main(String[] args)throws Exception {  
    String sDate1="31/12/1998";  
    Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);  
    System.out.println(sDate1+"\t"+date1);  
}  
}

Example 2: convert date to datetime java

Fixed it using this:

public DateTime dateAndTimeToDateTime(java.sql.Date date, java.sql.Time time) {
    String myDate = date + " " + time;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    java.util.Date utilDate = new java.util.Date();
    try {
        utilDate = sdf.parse(myDate);
    } catch (ParseException pe){
        pe.printStackTrace();
    }
    DateTime dateTime = new DateTime(utilDate);

    return dateTime;
}

Tags:

Php Example