-3

I am getting some JSON data from server that includes dates too. But it shows the date like this 2017-07-20 00:00:00 but I want to just see the date like this:2017-07-20, and i checked the previous questions about this issue but all of them were based on the date in the android side. And the problem is that I get the date as JSON and because of that I don't know how to remove Time from it.

Shayea
  • 51
  • 1
  • 6

3 Answers3

3

Did you try to simple parse this string like this?

String date_string = "2017-07-20 00:00:00";
String[] parsed = date_string.split(" ");
String your_wanted_string = parsed[0];
System.out.println(your_wanted_string);

EDIT

  1. You have to convert string into Date like here : https://stackoverflow.com/a/4216767/1979882
  2. Convert Date to milliseconds. Or use Calendar class.
  3. Calculate the difference between the values.

An example:

http://www.mkyong.com/java/how-do-get-time-in-milliseconds-in-java/

public class TimeMilisecond {
  public static void main(String[] argv) throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
    String dateInString = "22-01-2015 10:20:56";
    Date date = sdf.parse(dateInString);

    System.out.println(dateInString);
    System.out.println("Date - Time in milliseconds : " + date.getTime());

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    System.out.println("Calender - Time in milliseconds : " + calendar.getTimeInMillis());

  }
}
Community
  • 1
  • 1
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0
 String date_from_json="your date goes here";
 parseDate(date_from_json);

        public String parseDate(String s) {
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-mm-dd");
        Date date = null;
        String str = null;

    try {
        date = inputFormat.parse(s);
        str = outputFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return str;
}
Abilash
  • 59
  • 1
  • 9
0

You can use my javascript function to do this task from client side:

function formatDate(dateString) {
  var date = new Date("2017-07-20 00:00:00"),
    dd = date.getDate(),
    mm = date.getMonth() + 1,
    yyyy = date.getFullYear();
  mm = mm < 10 ? '0' + mm : mm;
  return dd + '-' + mm +'-' + yyyy;
}

call:

var dateStr = formatDate("2017-07-20 00:00:00");

demo

Anph
  • 163
  • 1
  • 14