1
String string_Date = "2014-06-11"; //my string date 
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");  
// Date startTimestam = simpleFormat.parse(string_Date); 
Timestamp startTimestam = Timestamp.valueOf(string_Date); 
Calendar cal = Calendar.getInstance(); 
cal.setTime(startTimestam);
cal.set(Calendar.HOUR_OF_DAY, 0); 
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 1);  
starTimeStamp = new Timestamp(cal.getTime().getTime()); 
// will get output date is "2014-06-11 00:00:01"

  • here I don't want use simple Format for converting string to date or timestamp,
  • if above code run I will get exception is illegalArgumentException i.e.

    Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]**

  • is it possible with out format converts string date.
  • if possible very helpfully for me.
zessx
  • 68,042
  • 28
  • 135
  • 158
user3599212
  • 429
  • 2
  • 6
  • 18
  • TimeStamp has its own for at requirements, so the only way to convert a String to a TimeStamp where the String does not match this requirement is through use of SimpleDateFormat to parse the String value to a Date value – MadProgrammer Jul 16 '14 at 08:07

1 Answers1

2

Your question is not very clear because of below:

1) Why do you want to avoid using SimpleDateFormat?

2) Do you want to convert String to Timestamp or Date to Timestamp?

If you want to convert String to Timestamp, then it is straightforward (without using SimpleDateFormat):

String timeValueStr="00:00:01";
String startTimeStr="2014-06-11" + " " + timeValueStr;
Timestamp startTimestamp = Timestamp.valueOf(startTimeStr);

If you want to convert String to Date and then Date to Timestamp:

//String to Date conversion
String startTimeStr = "2014-06-11"; 
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd"); 
Date d = simpleFormat.parse(startTimeStr);
//Date to Timestamp conversion
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 1);
Timestamp startTimestamp = new Timestamp(cal.getTime().getTime());

And of course, if you MUST avoid SimpleDateFormat, then here is the code to convert String to Timestamp first and then to Date (There is no way to convert String to Date directly without using SimpleDateFormat):

String timeValueStr="00:00:01";
String startTimeStr="2014-06-11" + " " + timeValueStr;
Timestamp startTimestamp = Timestamp.valueOf(startTimeStr);
Date d = new Date(startTimestamp.getTime());
Pat
  • 2,223
  • 4
  • 19
  • 25
  • thanks..nice explanation, its must avoid SimpleDateFormat. i tried like this : **'startTimeStr = startTimeStr.concat(" 00:00:01");'** then converted **'Timestamp.valueOf(startTimeStr);'**..its worked. – user3599212 Jul 16 '14 at 08:41