1

hi frnds i got some data from api service it contains date also the date format is 2013-09-06T14:15:11.557

which format it is??, how we can covert this date into 2013 sept 06 2:15

i used these 2 methods for coverting them to my date format

public static Date stringToDate(String dateString)
    {
        Date date = null;
        DateFormat df = new SimpleDateFormat(Constants.DATE_FORMAT_WITHOUT_TIME);
        try {
            date = df.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

and

public static String FormatDate(String dateString)
    {
        String s="";
        Date date = stringToDate(dateString);
        s = date.getDate()+" "+monthname[date.getMonth()+1]+" "+(date.getYear()+1900);
        return s;
        }

and this is my date format constant public static final String DATE_FORMAT_WITHOUT_TIME = "yyyy-MM-dd"; but it the parsing doesn't works correctly

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
ranjith
  • 4,526
  • 5
  • 27
  • 31

3 Answers3

5

You can try this

    String date="2013-09-06T14:15:11.557";
    DateFormat df=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    Date d=df.parse(date);
    df=new SimpleDateFormat("yyyy-MMM-dd hh:mm");
    System.out.println(df.format(d));

out put:

    2013-Sep-06 02:15
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
3
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");//Edit here depending on your requirements
dateFormat.setLenient(false);
String currentDateandTime1 = dateFormat.format(new Date());
Date dateObj = new Date();
dateObj = dateFormat.parse(stime);
Log.v("Hello", "" + dateFormat.format(dateObj));
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
Exceptional
  • 2,994
  • 1
  • 18
  • 25
0

Works like charm

public static String FormatDate(String dateString) {

        try {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
            Date d = sd.parse(dateString);
            sd=new SimpleDateFormat("yyyy' 'MMM' 'dd' 'hh:mm");
            System.out.println(sd.format(d));
            return sd.format(d);
        } catch (Exception e) {
        }
        return "";
    }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77