-2

I am new to android studio and java development.

I would like to parse this date:

"2017-04-26T20:55:00.000Z"

, which I am getting from a hash map string.

I would like to display only the date on my view.

G. Doe
  • 332
  • 2
  • 6
  • 15
  • Please clarify if you'd like to create an instance of Date class from a string? Or just modify the string to only keep the date part of it? There are 2 parts to your question as it seems to be: "How to create an instance of Date from a string?" and "How to print a date, given an instance of Date class?" –  Apr 26 '17 at 17:29

3 Answers3

35

You can always use Java's DateFormat API for achieving this. Here is the code snippet that will help you to achieve whatever task you are looking for.

 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
 Date date = dateFormat.parse("2017-04-26T20:55:00.000Z");//You will get date object relative to server/client timezone wherever it is parsed
 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); //If you need time just put specific format for time like 'HH:mm:ss'
 String dateStr = formatter.format(date);

You will get date object from which you can use it whichever way you would like to display using date formatter to format again.

Samarth
  • 773
  • 1
  • 6
  • 14
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Apr 26 '17 at 20:37
  • @BasilBourque My answer is according to Android where Java8 support is not yet fully there. `java.time` classes are only available after `Android platform version O`. Find here http://stackoverflow.com/questions/36000997/android-n-java8-java-time – Samarth Apr 27 '17 at 02:57
  • 1
    I should have mentioned… Much of the java.time  functionality is back-ported to a Java 6 and Java 7 in the [ThreeTen-Backport](http://www.threeten.org/threetenbp/) project, a project led by the same man who led both Joda-time and java.time, Stephen Colebourne. Further adapted for Android in the [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Apr 27 '17 at 03:24
0

In my opinion the cleanest way to do it :

  • extract the date as described here : Java string to date conversion
  • create a string containing the formatted date you want thanks to another DateFormat
  • display it!
Community
  • 1
  • 1
Epiliptik
  • 68
  • 6
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Apr 26 '17 at 20:38
-10

You could use substring like so

String date = yourString.substring(0, 10);

This would pull all the characters from 0 to 10 in your String and save it as a new String.

In this case that would return "2017-04-26"

Jeff.H
  • 403
  • 4
  • 14