Create date Timestamp for SQL from string in yyyy-MM-dd HH:mm:ss.SSS format - Android java.sql

Android examples for java.sql:Timestamp

Description

Create date Timestamp for SQL from string in yyyy-MM-dd HH:mm:ss.SSS format

Demo Code

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

public class Main {

  public static Timestamp date2Timestamp(String str) throws Exception {
    if (str == null || str.length() == 0)
      return null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Date date = sdf.parse(str);//  ww w .  ja va2  s  . c  o  m
    return new Timestamp(date.getTime());
  }

} 

Related Tutorials