0

I need to take date in the format date/month/year hours:minutes (time in 12 hour format) in java and I write the following code to save the date in datetoday. But it does not work for me. The output is shown in the following format Tue Nov 11 12:10:00 IST 2014. Can anyone help me regarding the issue? Thanks in advance.

Date datetoday;    
    void todaysdate() {                 
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("DD/MM/yyyy HH:mm");
                String inputString2 = sdf.format(date);
            try {
                datetoday = sdf.parse(inputString2);
            } catch (ParseException ex) {
                System.out.println(ex.getMessage());
            }
                System.out.println(datetoday);

        }
akshaivk
  • 427
  • 5
  • 24
  • `Date` has no concept of format, instead of "parsing" the date a second time, return the formatted `String` – MadProgrammer Nov 11 '14 at 06:48
  • You might find [this example](http://stackoverflow.com/questions/12575990/calendar-date-to-yyyy-mm-dd-format-in-java/12576219#12576219) useful... – MadProgrammer Nov 11 '14 at 06:49
  • 2
    Firstly, you want `dd` instead of `DD`. Next, you want `hh` instead of `HH` for a 12-hour clock... and you'll want an am/pm specifier too. Finally, a `Date` doesn't have a particular format - if you want to format it in a specific way, you should do so with another `DateFormat`. – Jon Skeet Nov 11 '14 at 06:49

1 Answers1

-1

You can try this

new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss z");

Use hh instead of HH to get 12 hours format

Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70