From a database I am getting a date which's format is like this
2012-7-8
but I want to convert this to look something like this
2012 July 8th
but I am unable to do this, is there any way?
You must first parse the date with A SimpleDateFormat object, converting string from database to a Date object. Then you shall format this date object with another instance of SimpleDateFormat, converting the date object to astring, formatted according to you choice.
You may check javadoc of SimpleDateFormat for details on how to specify the format.
public class DateFormatTest {
public static void main(String[] args) throws ParseException {
String dateFromDatebase = "2012-7-8";
SimpleDateFormat databaseFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = databaseFormat.parse(dateFromDatebase);
SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy MMMMM d");
String formattedDate = targetFormat.format(date);
System.out.println(dateFromDatebase + " -> " + formattedDate);
}}
This outputs 2012-7-8 -> 2012 July 8
Try this..... Just one line of code....
public class Test {
public static void main(String[] args){
System.out.println(new SimpleDateFormat("YYYY MMM dd").format(new Date()));
}
}