-4

To format date and time which get from sqLite like 2016-07-13 13:10:00. I want to format it like 07-Jul-2016, 01:10 PM.

I have tried lots of solution but I can't find any better format.

Thanks in advance.

Rifan
  • 424
  • 6
  • 17

2 Answers2

2
  String strCurrentDate= "2016-07-13 13:10:00";
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date newDate = null;
  try {
      newDate = format.parse(strCurrentDate);
      format = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");
      String date = format.format(newDate);
      System.out.println(date);
  } catch (ParseException e) {
      e.printStackTrace();
  }
Zeeshan Khan
  • 553
  • 7
  • 11
1
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");

String inputDateStr="2011-01-11";
Date date = inputFormat.parse(inputDateStr);
String outputDateStr = outputFormat.format(date);

from 2011-01-11 to 11 Jan 2011

refer to https://developer.android.com/reference/android/icu/text/SimpleDateFormat.html

to find the pattern of the date from your sql and the new pattern you want

TWL
  • 6,228
  • 29
  • 65
  • can I convert `11 Jan 2011` to `11-Jan-11` ? – Rifan Aug 16 '16 at 05:37
  • yes use `"dd-MMM-yy"` – TWL Aug 16 '16 at 05:43
  • Is there any solution to do the same operation with time format? I have such stringl ike `dd.MM.yyyy HH:mm` and I could change date format as above shown, but I can't not change the time format. How can I do? Thanks. – Mark Delphi Nov 21 '20 at 00:17