-2

I have a string like "2017-05-22 12:57:46.688" from which I have to separate date and want to change it's format like "dd/MM/yyyy"and set it to a textview.

Code is as below:

String transactionDate = post.getString(CEarningHistory.TRANSACTION_CTS);
String[] parts = transactionDate.split(" ");
SimpleDateFormat input = new SimpleDateFormat(parts[0]);
PVitt
  • 11,500
  • 5
  • 51
  • 85
Niraj
  • 107
  • 2
  • 14

2 Answers2

0

Try this

 SimpleDateFormat webFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  webFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

 SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
  dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String newDate =   dateFormat.format(webFormat.parse("YourdateinWebFormat"));
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Mohit Kacha
  • 484
  • 2
  • 12
-3

Try this way, you need to pass server date time format and output format.

String yourDate = formatDateForString(inputFormat, outputFormat, inputDateTime);

public String formatDateForString(String inputFormat, String outputFormat, String inputDate) {

        Date parsed;
        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) {
            LOGE("DateTime", "ParseException - dateFormat");
        }

        return outputDate.toUpperCase().replace(".","");

    }
Jd Prajapati
  • 1,953
  • 13
  • 24