-6

I am working on a Java application and I have the following problem trying to format a date.

So I inizialize an object field with a new java.util.Date; that represent the current date time, this one:

progetto.setDatOraUltMov(new Date());   

When I print this field the result is somethind like this:

Mon Oct 12 17:19:06 CEST 2015

Ok, this standard is not good for my pourpose and I want that is shown something like this:

12/10/2015 17:19:06

Something like in the format DAY/MONTH/YEAR HOUR:MINUTE:SECOND

How can I do something like this? How can I specify the required date format?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 2
    Take a look http://stackoverflow.com/questions/26602291/how-to-get-the-current-date-and-time-of-the-system-in-java/26602398#26602398 – Anptk Oct 13 '15 at 08:56
  • 1
    Did you google? [format java date](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=off&q=format+java+date) – Codebender Oct 13 '15 at 08:56

2 Answers2

1

Use SimpleDateFormat.

SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
DateToStr = format.format(curDate);

For your reference

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Ritt
  • 3,181
  • 3
  • 22
  • 51
0

You can try this

       Calendar currentDate = Calendar.getInstance();
       SimpleDateFormat formatter= new SimpleDateFormat("dd-MM-YYYY-hh:mm:ss");
       String dateNow = formatter.format(currentDate.getTime());
       System.out.println(dateNow);

import

import java.text.SimpleDateFormat;
import java.util.Calendar;
Anptk
  • 1,125
  • 2
  • 17
  • 28