Question

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.

Was it helpful?

Solution

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

date = date.substring(0,10);

OTHER TIPS

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 = " ";
        }

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);

Use substring method

String jsonString = "14-03-2014 09:02:10";
if(jsonString.contains(" ")){
   String onlyDate = string.substring(0, string.indexOf(" ")); 
}

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);
    }
}

Use the below code

String date="14-03-2014 09:02:10";
String reqdate=date.substring(0,10);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top