-2

I have a Date Object (Fri Dec 31 00:00:00 CET 1999) and i just want to display 31-12-1999.

And i don't want use String Object for display this information i need to display with a Date Object.

Do you got a solution for this problem ?

Joffrey Hernandez
  • 1,809
  • 3
  • 21
  • 39

3 Answers3

4

Use java.text.SimpleDateFormat.

SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = dateFormatter.format(dateInstance);
Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
1

You can write the Date as text directly to a ByteBuffer which you can write to an IO device. This can be used to speed up logging for example.

The logic for converting dates with timezones is so complex however, I would suggest you use the standard libraries or JodaTime for dates as its not worth trying to write yourself.

For this reason, I write times directly to a ByteBuffer and use SimpleDateFormat to produce a cached String for the date (as it only changes once per day)

If you want to display to a GUI, you will have to use a String because that what the GUI uses.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Do you mean you need to remove the time information from the Date object? You can do something like this.

Date d; // this is your date 
Date dateWithoutTime = new Date(d.getYear(), d.getMonth(), d.getDay());
Sameer
  • 4,379
  • 1
  • 23
  • 23