Mithun Khatri
Mithun Khatri

Reputation: 696

Format String to Timestamp

How to format a timestamp coming as an Object to another timestamp format.

I want to convert-

"2014-3-16.8.6.57.323000000" (type is Object) into Expected : "Mar 16, 2014 6:57:10 PM" (type is String)

Upvotes: 0

Views: 90

Answers (4)

Mithun Khatri
Mithun Khatri

Reputation: 696

Its fixed. Dual conversion was required--

DateFormat df = new SimpleDateFormat("yyyy-MM-dd.HH.mm.ss");
        try {
            Date d = df.parse("2014-3-16.8. 6. 57.323000000");
            Date d1 = new Date(d.getTime());
            String dd = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a")
                    .format(d1);
            System.out.println(dd);
        } catch (ParseException e) {
            e.printStackTrace();
        }

Upvotes: 0

pavithraCS
pavithraCS

Reputation: 691

Try this

Timestamp stamp = new Timestamp(System.currentTimeMillis());
Date d= new Date(stamp.getTime());
String date = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss").format(d);
System.out.println(date);

Upvotes: 1

Smit Shilu
Smit Shilu

Reputation: 355

you can also use this for convert current date and time as timestamp

Date d = new Date();
String dd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);

Upvotes: 1

anirudh
anirudh

Reputation: 4176

You can convert it using the SimpleDateFormat class.

Have a look at http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Upvotes: 0

Related Questions