Convert String Date to String date different format

Murugesapillai Nagkeeran picture Murugesapillai Nagkeeran · Feb 21, 2013 · Viewed 56.7k times · Source

I am new to Java. Postgres db contain date format is yyyy-MM-dd. I need to convert to dd-MM-yyyy.

I have tried this, but wrong result is display

   public static void main(String[] args) throws ParseException {

    String strDate = "2013-02-21";
      DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
      Date da = (Date)formatter.parse(strDate);
      System.out.println("==Date is ==" + da);
      String strDateTime = formatter.format(da);

      System.out.println("==String date is : " + strDateTime);
}

Answer

asifsid88 picture asifsid88 · Feb 21, 2013
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("2013-02-21");
System.out.println(format2.format(date));