5

My string date --> 2016-10-02T00:00:00.000Z. I want to get only date from this string. I tried to parse through below coding but it throws me error! I have exactly the same format as mentioned in the string. Any answers?

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        try {
            Date myDate = sdf.parse( dateofJoining.replaceAll( "([0-9\\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})", "$1$2" ) );
            System.out.println("Date only"+ myDate );
        } catch (ParseException e) {
            e.printStackTrace();
        }

I also tired below code,

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
    Date date = format.parse(dtStart);  
    System.out.println(date);  
} catch (ParseException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}

The error which i get

java.text.ParseException: Unparseable date: "2016-10-02T00:00:00.000Z" (at offset 19)
05-12 00:18:36.613 4330-4330/com.vroom.riderb2b W/System.err:     at java.text.DateFormat.parse
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
KarthikKPN
  • 653
  • 12
  • 22
  • Ooh, this would come much more naturally if you could use `java.time` on Android. `Instant.parse("2016-10-02T00:00:00.000Z").atOffset(ZoneOffset.UTC).toLocalDate()`. Well, you can if you get [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP); all the date and time classes you need are there. – Ole V.V. May 11 '17 at 21:13

5 Answers5

11

change the simple date format to use: yyyy-MM-dd'T'HH:mm:ss.SSSZ

in your code:

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");  
try {  
    Date date = format.parse(dtStart.replaceAll("Z$", "+0000"));  
    System.out.println(date);  
} catch (ParseException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}

If you want to get date/mm/yy from it:

use:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
// use UTC as timezone
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Log.i("DATE", sdf.format(date));   //previous date object parsed

if you want output format: hour:minute AM/PM

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(date));

EDIT

More easier option is to split the string in two parts like:

String dateString = "2016-10-02T00:00:00.000Z";
String[] separated = dateString.split("T");
separated[0]; // this will contain "2016-10-02"
separated[1]; // this will contain "00:00:00.000Z"
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
3

try with this :

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
X6Entrepreneur
  • 971
  • 2
  • 10
  • 30
  • Its again giving date with time stamp: Sun Oct 02 05:30:00 GMT+05:30 2016 I just need to get date/mm/yy from it – KarthikKPN May 11 '17 at 19:01
0

Try

Calendar calendar = Calendar.getInstance();
TimeZone localTZ = calendar.getTimeZone();    
String format1 = "yyyy-MM-dd"; //will return 2017-01-31
String format2 = "dd"; //will return DAY only like 31
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(localTZ );
String result = sdf.format(your_date);
Max
  • 146
  • 2
  • 9
0

For me it's worked like this:

textView_last_comm.setText(parseDateFormat(passDetailsModel.getLast_comm(), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "dd/MM/yy HH:mm"));

  public static String parseDateFormat(String dateToFormat, String inputFormat, String outputFormat) {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputFormat);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputFormat);

Date date = null;
String str = null;

try {
    date = inputFormat.parse(dateToFormat);
    str = outputFormat.format(date);
} catch (ParseException e) {
    e.printStackTrace();
}
return str;
}
Anil Kumar
  • 1,830
  • 15
  • 24
-1

You can get date easily by using String.substring() method:

String string = "2016-10-02T00:00:00.000Z";
String date = string.substring(0, 10);

Log.d("SUCCESS", "DATE: " + date);

OUTPUT:

D/SUCCESS: DATE: 2016-10-02
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • 1
    The question seemed to ask for a `Date` object rather than a `String`. Also one would prefer to have the input validation that lies in actaully parsing the input rather than blindly taking a substring. Also a little more explanation may be helpful. – Ole V.V. May 11 '17 at 21:27
  • OP is asking about "I want to get only date from this string?". He is not asking about any specific date format. So what's wrong with my answer. – Ferdous Ahamed May 11 '17 at 21:31