2

Using below code , the output is 15-Mar-2016 06:24:41.296PM

Date sDate=new Date();
SimpleDateFormat pattern = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss.SSSa");
System.out.println(pattern .format(sDate));

If I want the output as 15-Mar-2016 06:00:00.0PM is it possible?

user2359634
  • 1,275
  • 5
  • 14
  • 27
  • 2
    You may want to first reset the mm:ss.SSS part of your `Date` , see here : http://stackoverflow.com/questions/17821601/set-time-to-000000 – Arnaud Mar 15 '16 at 13:01
  • instead of doing a `new Date()` get a `Calendar` instance and set the mins and secs to 0. – Sachin Mar 15 '16 at 13:02
  • 1
    So actually you want to round your date to hour resolution? What if the time is 5.59.59 ? – leonbloy Mar 15 '16 at 13:21

2 Answers2

1

Rather than using a Calendar and resetting portions of the date to zero, you can use single quotes to insert literal strings in the formatted date:

SimpleDateFormat pattern = new SimpleDateFormat("dd-MMM-yyyy hh:'00:00.0'a");

Demo

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • To avoid any confusion, you could precise in your answer that the date in itself won't be changed, only the visual representation given by the formatter will be altered. – Spotted Mar 15 '16 at 13:17
  • 1
    @Spotted I am not sure where the confusion is - `SimpleDateFormat` never changes the date it is passed. – Andy Turner Mar 15 '16 at 13:19
  • I thought it could be confusing if you examine the output of this program: `Date d = new Date(); SimpleDateFormat pattern = new SimpleDateFormat("dd-MMM-yyyy hh:'00:00.0'a"); System.out.println(pattern.format(d)); System.out.println(d);` which give 2 different visual representations of the same object. – Spotted Mar 15 '16 at 13:24
  • Why would you be using `SimpleDateFormat` in the first place if you found that `Date.toString()` met your requirements? – Andy Turner Mar 15 '16 at 13:30
  • As a human, you could forget to use a `SimpleDateFormat` for a future use. Thus having a regression in your codebase because the generated representation doesn't match the expected one anymore. – Spotted Mar 15 '16 at 13:41
  • That has absolutely nothing to do with whether the date is changed by using a `SimpleDateFormat`. – Andy Turner Mar 15 '16 at 13:42
  • Could we continue the discussion on a chat ? I think I failed to express my opinion correctly. – Spotted Mar 15 '16 at 13:45
0

Depending on whether you want to truncate (i.e., floor) or round to the nearest hour, you could do one of the following:

    final long MS_PER_HOUR = 1000L * 60 * 60;
    SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss.SSSa");

    long now = System.currentTimeMillis();
    long trunc = (now / MS_PER_HOUR) * MS_PER_HOUR;
    long round = ((now + MS_PER_HOUR / 2) / MS_PER_HOUR) * MS_PER_HOUR;

    System.out.println("now:   " + fmt.format(new Date(now)));
    System.out.println("trunc: " + fmt.format(new Date(trunc)));
    System.out.println("round: " + fmt.format(new Date(round)));

Which will generate this output for a time between 09:00:00 and 09:29:59

now:   15-Mar-2016 09:15:37.721AM
trunc: 15-Mar-2016 09:00:00.000AM
round: 15-Mar-2016 09:00:00.000AM

and, for a time between 09:30:00 and 09:59:59

now:   15-Mar-2016 09:45:50.925AM
trunc: 15-Mar-2016 09:00:00.000AM
round: 15-Mar-2016 10:00:00.000AM
Mike Harris
  • 1,487
  • 13
  • 20