3/11/09

DATE TIME in JAVA

Form VS Database Mysql (Timestamp)

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

public class date_time {

public static void main ( String [ ] args ) {
try
{
// String to Timestamp
String st = "16/08/2007 09:04:34";
SimpleDateFormat form_sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Date form_date = form_sdf.parse(st);
Timestamp timestamp = new Timestamp(form_date.getTime());
System.out.println(timestamp.toString());

//Timestamp to String
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = sdf.parse("2005-04-06 09:01:10");
System.out.println(date.getDate() + "/" + (int)(date.getMonth() + 1) + "/" + (int) (date.getYear() + 1900));
}catch (Exception e) {
// TODO: handle exception
System.out.print(e);
}
}
}

No comments: