0

I want the current date and time in the following format :

Date :YYYYMMDD

Time : HHMMSS

I tried the following

DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
   //get current date time with Date()
   Date date = new Date();
   System.out.println(dateFormat.format(date));

   //get current date time with Calendar()
   Calendar cal = Calendar.getInstance();
   System.out.println(new Date().getTime());

By this I am getting the desired date output but the time is coming in this way 1341837848290.

The expected is HHMMSS.

Beginner
  • 855
  • 9
  • 21
  • 37
  • possible duplicate of [String to Date in Different Format in Java](http://stackoverflow.com/questions/882420/string-to-date-in-different-format-in-java) – Joachim Sauer Jul 09 '12 at 12:52

5 Answers5

9

Use format()

System.out.println(new SimpleDateFormat("HH:mm:SS").format(new Date()));

Date instance doesn't have any property to hold custom format, So you need to format the date instance to String with your custom format HH:mm:SS (See API doc for more detail)

See

jmj
  • 237,923
  • 42
  • 401
  • 438
  • +1 I would use `System.currentTimeMillis()` rather than create a `Date` object just to get the time. You can pass `new Date()` to format alone. – Peter Lawrey Jul 09 '12 at 12:54
  • (s)he doesn't want this format and (s)he already uses format()... also the format you propose is wrong... – peshkira Jul 09 '12 at 12:57
  • @peshkira `System.out.println(new Date().getTime());` please re read the question – jmj Jul 09 '12 at 12:58
  • yes, I have.. this is just because the user believes that getTime() returns the time and not a long representation of the whole date... The correct answer would be to use two different DateFormatters as @Rajesh answered. and your time format pattern is still not the same as in the question... – peshkira Jul 09 '12 at 13:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13611/discussion-between-peshkira-and-jigar-joshi) – peshkira Jul 09 '12 at 13:02
  • "HH:mm:SS" should be "HH:mm:ss". `SS` is for milliseconds (within second), `ss` is for seconds. – Artsiom Chapialiou Apr 01 '20 at 05:35
2
try this 

DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
   //get current date time with Date()
   Date date = new Date();
   System.out.println(dateFormat.format(date));

   //get current date time with Calendar()
DateFormat timeFormat = new SimpleDateFormat("HHmmss");
   Date d=new Date();
   System.out.println(timeFormat.format(d);
Rajesh
  • 2,934
  • 8
  • 42
  • 71
0

Did you check out the joda-time library? Link here

With joda-time, you could easily call new DateTime(), call toString() on it and have this output, which may be more what you want:

public static void main(final String[] args) {
        final DateTime d = new DateTime();
        System.out.println(d.toString());
    }

Output: 2012-07-09T14:54:13.366+02:00

Joda-Time is very powerful on the plus side. Of course, this is an extra lib you need to include, and if this is not possible or desired, another approach would probably be better.

Scorpio
  • 2,309
  • 1
  • 27
  • 45
0

I tried this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
Date date = Calendar.getInstance().getTime();
System.out.println(sdf.format(date));

Yields:

20120709 145518

First section is the date (20120709), the second section is the time(145518).

It seems that you have been using the wrong notation. I would recommend you take a look here for full details.

npinti
  • 51,780
  • 5
  • 72
  • 96
0
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:SS");
    //get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));

for more formatting refer API Doc

swapy
  • 1,616
  • 1
  • 15
  • 31