I am to new to Java. I want to display date in dd/mm/yyyy
format. When I print date using new Date()
function it gives me Sting which I don't want. Say I want to print today's date it gives me Sat Jan 24 08:17:41 IST 2015
but I want output as 24/1/2015
Asked
Active
Viewed 1.3k times
0

Nerdroid
- 13,398
- 5
- 58
- 69

Savio Menezes
- 13
- 1
- 1
- 4
-
Dude, there are *hundreds* of existing SO Question on converting dates to strings in specific formats. Why couldn't you search for and read one of those? Please always search before you ask. It saves everyone's time ... – Stephen C Jan 24 '15 at 03:24
2 Answers
6
Use SimpleDateFormat
:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());

Jean Logeart
- 52,687
- 11
- 83
- 118
0
There you have one way to do it: http://docs.oracle.com/javase/tutorial/datetime/iso/format.html
So basically you can define the format in an formatter and use that to convert the date into a string:
try {
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");
String out = departure.format(format);
System.out.printf("LEAVING: %s (%s)%n", out, leavingZone);
}
catch (DateTimeException exc) {
System.out.printf("%s can't be formatted!%n", departure);
throw exc;
}

Ayonix
- 1,872
- 1
- 15
- 14