Here you can find the source of toTimeStamp(Date date, String time)
public static Timestamp toTimeStamp(Date date, String time)
//package com.java2s; import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; public class Main { public static Timestamp toTimeStamp(Date date, String time) { Calendar cal = Calendar.getInstance(); cal.setLenient(true);//from w w w. j a va 2s.c o m boolean formatIs12 = time.endsWith("PM") || time.endsWith("AM"); String[] hhmmss = time.split(":"); if (hhmmss.length < 2) throw new IllegalArgumentException("invalid time"); if (hhmmss.length == 2) { String[] newTime = new String[3]; newTime[0] = hhmmss[0]; String[] temp = hhmmss[1].split(" "); newTime[1] = temp[0]; newTime[2] = "00"; if (formatIs12) newTime[2] += " " + temp[1]; hhmmss = newTime; } int hh = Integer.parseInt(hhmmss[0]); if (formatIs12 && time.endsWith("PM") && hh != 12) hh += 12; cal.set(Calendar.HOUR_OF_DAY, hh); cal.set(Calendar.MINUTE, Integer.parseInt(hhmmss[1])); if (hhmmss.length == 3) { String ss = hhmmss[2]; if (formatIs12) ss = ss.split(" ")[0]; cal.set(Calendar.SECOND, Integer.parseInt(ss)); } cal.set(Calendar.MILLISECOND, 0); return new Timestamp(cal.getTimeInMillis()); } }