how to convert date to dd-mon-yyyy format in java

how to convert date to dd-mon-yyyy format in java

To convert a date to the "dd-mon-yyyy" format in Java, you can use the SimpleDateFormat class and its format method, which formats a date according to a given pattern.

Here is an example of how you can use the SimpleDateFormat class to convert a date to the "dd-mon-yyyy" format:

refe‮r‬ to:lautturi.com
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = formatter.format(date);

This code creates a Date object representing the current date and time, and a SimpleDateFormat object with the pattern "dd-MMM-yyyy", which represents a two-digit day of the month, a three-letter abbreviation for the month, and a four-digit year. It then calls the format method of the formatter object and passes the date object as an argument. The result is a string in the "dd-mon-yyyy" format, which is stored in the formattedDate variable.

You can also use the SimpleDateFormat class to convert a Calendar object to the "dd-mon-yyyy" format, like this:

Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = formatter.format(calendar.getTime());

This code creates a Calendar object representing the current date and time, and a SimpleDateFormat object with the pattern "dd-MMM-yyyy". It then calls the getTime method of the calendar object to get the corresponding Date object, and passes it to the format method of the formatter object. The result is a string in the "dd-mon-yyyy" format.

Created Time:2017-11-01 12:05:14  Author:lautturi