-1

I getting a crash while getting this date format from server side and then i have been analyzed in google but didn't get any right solution for this.How to convert this date format to this way attached below Crash occurred in "startDate":"2018-01-23T00:00:00.000-05:00" this date

Expected date format:"2018-01-24 00:43:10 -0500" Suggest some possible solutions.

 SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    inFormat.setTimeZone(TimeZone.getDefault());
    String date = null;
    try {
        Date toConvert = inFormat.parse(OurDate);
        date = inFormat.format(toConvert);
    } catch (ParseException e) {         
        e.printStackTrace();
    }
    return date.toString();
}
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49

2 Answers2

0

Here you are with a working snippet of what you want to achieve:

public class FormatDateExample {
    public static void main(String[] args) {
        String date =  "2016-02-26T00:00:00+02:00";
        System.out.println(formatDate(date));

    }

    public static String formatDate(String unFormattedTime) {
         String formattedTime;
         try {
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
             Date date = sdf.parse(unFormattedTime);

             sdf = new SimpleDateFormat("dd MMM HH:mm");
             formattedTime = sdf.format(date);

             return formattedTime;

        } catch (ParseException e) {
             e.printStackTrace();
        }

        return "";
    }
}

First you have to parse the date with the given format you have

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = sdf.parse(unFormattedTime);

Then you have to format that date to the desired format "dd MMM HH:mm"

sdf = new SimpleDateFormat("dd MMM HH:mm");
formattedTime = sdf.format(date);

Source.. https://stackoverflow.com/a/35500350/3790052

SRB Bans
  • 3,096
  • 1
  • 10
  • 21
0

Try this

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat output1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

Date d = null;
try 
{
      d = input.parse("2018-01-23T00:00:00.000-05:00");

} 
catch (ParseException e) 
{
    e.printStackTrace();
}
String formatted = output.format(d);
Log.i("DATE", "" + formatted);

String formatted1 = output1.format(d);
Log.i("DATE1", "" + formatted1);

OUTPUT

I/DATE: 23/01/2018

I/DATE1: 2018-01-23 00:00:00 +0530
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31