-1

I have the following date

Date now = new Date(System.currentTimeMillis());

At the moment, when i display it in a listview, it has a value of

 2014-07-30

How can i specify the date object to be in the following format?

 2014-07-30 21:30:30.252

Is it possible to create a date object that has this format or is it a case of formatting the date object in the view?

thanks in advance

turtleboy
  • 8,210
  • 27
  • 100
  • 199
  • 2
    `SimpleDateFormat javadoc` google – jmj Jul 30 '14 at 20:34
  • 3
    There is no point in passing `System.currentTimeMillis()` into the `Date` constructor. You get the same thing with just `new Date()`. – David Conrad Jul 30 '14 at 20:43
  • **"At the moment it is has the value of 2014-07-30"** : Huh? How are you getting that date format? A `Date` object created using `System.currentTimeMillis()` has millisecond precision. – Squonk Jul 30 '14 at 20:44
  • possible duplicate of [SimpleDateFormat](http://stackoverflow.com/questions/3056703/simpledateformat) – Henry Keiter Jul 31 '14 at 04:34

4 Answers4

1

Use df.format

you need to use df.format(Date) method to get date in required format

Eg:

 DateFormat df = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss.SSS");
 String dateStr = df.format(new Date(System.currentTimeMillis()));

For more patterns, visit this page

Here's some explanation

Is it possible to get Date object with specific format?

No. Date doesn't have any format. It represents the number of milliseconds since epoch. You only get the formatted string using SimpleDateFormat, which you can get as above.

Community
  • 1
  • 1
Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
0

I think you want a SimpleDateFormat. The pattern you're looking for isn't too far off from the examples on that page.

sparks
  • 736
  • 1
  • 9
  • 29
0
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
format.format(now);
ScottJShea
  • 7,041
  • 11
  • 44
  • 67
bdub13
  • 1
  • Sorry i didn'y explain correctly. I would like a Date object in that format – turtleboy Jul 30 '14 at 20:39
  • You need to parse a Date you get in that format? A Date is an object, and the format makes sense when serializing and deserializing, but I guess I don't understand what you are asking. – bdub13 Jul 30 '14 at 20:42
0

DateFormat df = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss.SSS"); System.out.println(df.format(new Date(System.currentTimeMillis())));

Here's the output of that for me:

2014-07-30 16:39:23.092

mistahenry
  • 8,554
  • 3
  • 27
  • 38
  • Thanks, sorry i didn't explain correctly. i need a date object in that format – turtleboy Jul 30 '14 at 20:41
  • Check my answer, for that :) – Dheeraj Bhaskar Jul 30 '14 at 20:41
  • 3
    A `Date` object can't be in any format. It's just an instant in time, represented as a number. Only a String can be in some format. A `DateFormat` turns a `Date` into a `String`. – David Conrad Jul 30 '14 at 20:42
  • 1
    Date objects are just time since the unix epoch @DheerajBhaskar – mistahenry Jul 30 '14 at 20:42
  • 2
    @turtleboy from the java api, a Date is: "A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT." Date's have no format, you simply format them on output – mistahenry Jul 30 '14 at 20:46