private String pattern = "dd-MM-yyyy";
String dateInString =new SimpleDateFormat(pattern).format(new Date());
Answer from Balconsky on Stack OverflowUsing java.time.LocalDate,
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
System.out.println(dtf.format(localDate)); //2016/11/16
Use DateTimeFormatter to format the date as you want.
In your case the pattern is "dd/MM/yyyy".
Info
Java 8 introduced new APIs for Date and Time to address the shortcomings of the older java.util.Date and java.util.Calendar. The core classes of the new Java 8 project that are part of the java.time package like LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Period, Duration and their supported APIs.
The LocalDate provides various utility methods to obtain a variety of information. For example:
1) The following code snippet gets the current local date and adds one day:
LocalDate tomorrow = LocalDate.now().plusDays(1);
2) This example obtains the current date and subtracts one month. Note how it accepts an enum as the time unit:
LocalDate previousMonthSameDay = LocalDate.now().minus(1, ChronoUnit.MONTHS);
If you are using JAVA 8 you can use LocalDateTime class and DateTimeFormatter.
see below example using JAVA 8:
LocalDateTime now = LocalDateTime.now();
System.out.println("Current DateTime Before Formatting: " + now);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formatedDateTime = now.format(formatter);
System.out.println("Current DateTime after Formatting:: " + formatedDateTime );
Better Approach
Simply Use SimpleDateFormat
new SimpleDateFormat("MM/dd/yyyy").format(new Date(timeStampMillisInLong));
Mistake in your Approach
DAY_OF_MONTH ,MONTH, .. etc are just constant int value used by Calendar class
internally
You can get the date represented by cal by cal.get(Calendar.DATE)
Use the SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String time = sdf.format(date);