Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Convert String to TimeStamp

I have an issue while I try to convert a String to a TimeStamp. I have an array that has the date in the format of yyyy-MM-dd and I want to change in the format of yyyy-MM-dd HH:mm:ss.SSS. So, I use this code:

final String OLD_FORMAT = "yyyy-MM-dd"; final String NEW_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; String oldDateString = createdArray[k]; String newDateString;  DateFormat formatter = new SimpleDateFormat(OLD_FORMAT); Date d = formatter.parse(oldDateString); ((SimpleDateFormat) formatter).applyPattern(NEW_FORMAT); newDateString = formatter.format(d); System.out.println(newDateString);  Timestamp ts = Timestamp.valueOf(newDateString); System.out.println(ts); 

and I get the following result.

2009-10-20 00:00:00.000

2009-10-20 00:00:00.0

but when I try to simply do

String text = "2011-10-02 18:48:05.123"; ts = Timestamp.valueOf(text); System.out.println(ts); 

I get the right result:

2011-10-02 18:48:05.123

Do u know what I might be doing wrong? Thanks for the help.

like image 556
Dimitra Micha Avatar asked Sep 20 '13 10:09

Dimitra Micha


People also ask

Can we convert string to timestamp in Java?

Use TimeStamp. valueOf() to Convert a String to Timestamp in Java. We will use the TimeStamp class's own static function - valueOf() . It takes a string as an argument and then converts it to a timestamp.

Can we convert string to timestamp?

To convert a date string to a timestamp: Pass the date string to the Date() constructor. Call the getTime() method on the Date object. The getTime method returns the number of milliseconds since the Unix Epoch.

Is timestamp a string?

A string representation of a timestamp is a character or a Unicode graphic string that starts with a digit and has a length of at least 14 characters.

How do I convert a string to a Date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.


2 Answers

Follow these steps for a correct result:

try {     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");     Date parsedDate = dateFormat.parse(yourString);     Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime()); } catch(Exception e) { //this generic but you can control another types of exception     // look the origin of excption  } 

Please note that .parse(String) might throw a ParseException.

like image 141
ZaoTaoBao Avatar answered Sep 20 '22 17:09

ZaoTaoBao


import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;  public class Util {   public static Timestamp convertStringToTimestamp(String strDate) {     try {       DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");        // you can change format of date       Date date = formatter.parse(strDate);       Timestamp timeStampDate = new Timestamp(date.getTime());        return timeStampDate;     } catch (ParseException e) {       System.out.println("Exception :" + e);       return null;     }   } } 
like image 23
Harsh Avatar answered Sep 22 '22 17:09

Harsh