-2

I am getting this 14-03-2014 09:02:10 string from JSON. All is OK but I want just the date part in this string not the time (14-03-2014). So how can I convert the string to this format.

Abid Khan
  • 2,451
  • 4
  • 22
  • 45

6 Answers6

0

You just try this solution i hope it will useful for you just give the date format into one you can get the desired one...

String inputPattern = "yyyy-MM-dd hh:mm:ss";
        String outputPattern = "dd/MM";
        SimpleDateFormat inFormat = new SimpleDateFormat(inputPattern);
        SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
        Date date1 = null;

        if (date != null) {
            try {
                date1 = inFormat.parse(date);
                str = outputFormat.format(date1);
                Log.d(null, str);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        } else {
            str = " ";
        }
Naveen Kumar Kuppan
  • 1,424
  • 1
  • 10
  • 12
0

If you already have the String, a substring would do the work:

date = date.substring(0,10);
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
0

You can use Split() method for extract date part from String

String str = "14-03-2014 09:02:10"
String [] part = str.split(" ");
String date = part[0];

Or you can also use substring() method if your date field's size is constant, i.e. for 01 instead of 1 in date part

str = str.substring(0,10);
Lucifer
  • 29,392
  • 25
  • 90
  • 143
0

Use substring method

String jsonString = "14-03-2014 09:02:10";
if(jsonString.contains(" ")){
   String onlyDate = string.substring(0, string.indexOf(" ")); 
}
Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
0

Try this...

public class Test {

    public static void main(String a[])
    {
        String date_time = "14-03-2014 09:02:10";
        String[] split_val = date_time.split(" ");
        String date = split_val[0];
        String time = split_val[1];
        System.out.println(date+"--------------"+time);
    }
}
Giridharan
  • 4,402
  • 5
  • 27
  • 30
0

Use the below code

String date="14-03-2014 09:02:10";
String reqdate=date.substring(0,10);
jyomin
  • 1,957
  • 2
  • 11
  • 27