2

I have tried below code:

String created_Date = "25-Nov-15 14:23:34";

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");

System.out.println("Formatted date====>"+sdf.format(created_Date));

tv_date.setText(sdf.format(created_Date));

I am getting

llegalArgumentException: Bad class: class java.lang.String

Uttam Kumar Roy
  • 2,060
  • 4
  • 23
  • 29
JayJoshi
  • 81
  • 1
  • 1
  • 9
  • Possible duplicate of [How do you format date and time in Android?](http://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android) – AL. May 18 '16 at 06:53
  • Or duplicate of http://stackoverflow.com/questions/10426492/change-date-string-format-in-android – Alexis May 18 '16 at 07:31

5 Answers5

4

First thing is your date value in not in dd-MM-yyyy HH:mm:ss in format, it's should like as dd-MMM-yy HH:mm:ss. Your date have only two digit in year and three character in month.

here is sample code for change date format.

String mStringDate = "25-Nov-15 14:23:34";
String oldFormat= "dd-MMM-yy HH:mm:ss";
String newFormat= "dd-MM-yyyy HH:mm";

String formatedDate = "";
SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
Date myDate = null;
try {
       myDate = dateFormat.parse(mStringDate);
} catch (java.text.ParseException e) {
     e.printStackTrace();
}

SimpleDateFormat timeFormat = new SimpleDateFormat(newFormat);
formatedDate = timeFormat.format(myDate);

tv_date.setText(formatedDate );
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
0

Use below code to get any kinda date format

/**
 * This method is used for change date formate
 */
public static String getRequiedformatDate(String serverFormate,
                                          String requiredFormate, String ourDate) {
    DateFormat theDateFormat = new SimpleDateFormat(serverFormate,Locale.getDefault());
    Date date = null;
    String dateStr = "";
    try {
        if (ourDate != null && !ourDate.equals("null")) {
            date = theDateFormat.parse(ourDate);
            theDateFormat = new SimpleDateFormat(requiredFormate,Locale.ENGLISH);
            dateStr = theDateFormat.format(date).toString();
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return dateStr;
}
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
0

Try this.

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy hh:mm:ss");
Date newDate;
String date = "";
try {
    newDate = format.parse(String created_Date);
    format = new SimpleDateFormat("dd-MM-yyyy hh:mm");
    date = format.format(newDate);
} catch (ParseException e) {
    e.printStackTrace();
}
System.out.println("Formatted date====>"+ date);

Happy coding..

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
0

Try this,

String strCurrentDate = "Wed, 18 May 2016 07:55:29 +0000";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
Date newDate = format.parse(strCurrentDate);

format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(newDate);

hope this will helps you.

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0
public class DateFormater {

    public static String formatDateFromString(String inputFormat, String outputFormat, String inputDate) {

        Date parsed = null;
        String outputDate = "";


        SimpleDateFormat df_input = new SimpleDateFormat(inputFormat);
        SimpleDateFormat df_output = new SimpleDateFormat(outputFormat);

        try {
            parsed = df_input.parse(inputDate);
            outputDate = df_output.format(parsed);

        } catch (ParseException e) {
            Log.d("CALENDER", "ParseException - dateFormat");
        }

        return outputDate;

    }

}
The_Martian
  • 3,684
  • 5
  • 33
  • 61