0

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?

Reyjohn
  • 2,654
  • 9
  • 37
  • 63

3 Answers3

2

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

Kurtcebe Eroglu
  • 1,934
  • 11
  • 15
  • can you please provide me a snippet of code of the method that will work? – Reyjohn Jul 08 '12 at 15:51
  • Well what you kind of overlooked was the requirement to meet '8th'. So I think it should also be '1st' and '2nd' etcpp. If i am right, Reyjohn might find his answer to this part of his problem right here : http://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-in-java – Daniel Leschkowski Jul 08 '12 at 19:37
1

You can use Simple Date Formatter

JR Galia
  • 17,229
  • 19
  • 92
  • 144
0

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()));
      }
  }
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75