Techie Hints

A place where to find tech solution

How to get Timestamp from String in Java ?

Assume for the String in format ‘YYYY-MM-DD HH:MM AM/PM‘ needs to be converted into ‘java.sql.Timestamp‘ object

1) Construct a string in the format: YYYY-MM-DD HH:MM AM/PM.

int year = 2015;
int month = 12;
int date = 23;
int time = 12;
int min = 30;
String meridiem = “pm”;
String dateStr = year+”-“+month+”-“+date+” “+time+”:”+min+” “+meridiem;

2) Now I use java.text.SimpleDateFormat class to parse the string.

DateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd hh:mm aa”);

See below page for Letter explaination.

3) Parse the string using DateFormat object to get Date util date.

Use the string constructed on 1) here it is  dateStr

Date date = dateFormat.parse(dateStr);

4) Just convert java.util.Date into java.sql.Timestamp

Timestamp timestamp = new Timestamp(date.getTime());


I suggest to construct a method in your project util class and reuse every time when ever it is required.

public static Timestamp getSQLTimestamp(String dateString) throws ParseException {
     DateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd hh:mm aa”); 
     Date date = dateFormat.parse(dateString);
     long time = date.getTime();

     return new Timestamp(time);


Refer below table for Letter definition you can define for Date formats in java using java.text.SimpleDateFormat

Letter         Date or Time Component      Presentation          Examples
G                Era designator                             Text                          AD
y                 Year                                              Year                          1996; 96
M                Month in year                           Month                      July; Jul; 07
w                Week in year                              Number                   27
W               Week in month                         Number                   2
D                Day in year                                 Number                    189
d                 Day in month                            Number                    10
F                 Day of week in month            Number                    2
E                 Day in week                               Text                            Tuesday; Tue
a                 Am/pm marker                         Text                            PM
H                Hour in day (0-23)                   Number                   0
k                 Hour in day (1-24)                   Number                    24
K                 Hour in am/pm (0-11)            Number                    0
h                 Hour in am/pm (1-12)            Number                    12
m                Minute in hour                         Number                    30
s                  Second in minute                    Number                    55
S                  Millisecond                               Number                    978
z                  Time zone                                  General time zone Pacific Standard Time; PST; GMT-08:00
Z                 Time zone                                  RFC 822 time zone -0800

(from Java API doc)

Thanks for referring this link, Hope it is helpful.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: