How to convert date format in java?

2.15K viewsJava
0

how to convert date format in java ,i want to convert the date format.

Example : dd-mm-yyyy to dd/mm/yyyy

Changed status to publish
Add a Comment
0

U can use the below method for changing the date format :

public static String formatDate(String dateString, String fromDate, String toDate) {
		DateFormat readFormat = new SimpleDateFormat(fromDate);
		DateFormat writeFormat = new SimpleDateFormat(toDate);
		Date date = null;
		try {
			date = readFormat.parse(dateString);
		} catch (ParseException e) {
			// here write the logs for identifying the runntime errors
		}
		String formattedDate = "";
		if (date != null) {
			formattedDate = writeFormat.format(date);
		}
		return formattedDate;
	}

 

Changed status to publish
Add a Comment
Write your answer.