TL; DR
Instant.parse( "2015-08-20T08:26:21.000Z" ) .toString()
2015-08-20T08: 26: 21Z
Formatting Date and Time
If all you want to do is to exclude .000 , then use time-time objects to parse your input string value, and then generate a new string representation of that date and time value in a different format.
ISO 8601
By the way, if this is your goal, the name of the Questions does not make sense, since both lines mentioned in the first sentence are valid ISO 8601 formatted lines.
2015-08-20T08:26:21.000Z2015-08-20T08:26:21Z
java.time
Java 8 and later have the new java.time package . These new classes replace the old java.util.Date/.Calendar and java.text.SimpleDateFormat classes. These old classes were confusing, troublesome, and erroneous.
Instant
If you want only the UTC time zone, you can use the Instant class. This class represents a point on the timeline without taking into account any specific time zone (mainly UTC).
DateTimeFormatter.ISO_INSTANT
Calling instances of toString generates a string representation of the date and time value using the DateTimeFormatter.ISO_INSTANT formatting instance. This formatter automatically dies relative to a fractional second. If the value is a whole second, no decimal places are generated (obviously what the Question wants). For a fractional second, digits are displayed in groups of 3, 6, or 9, if necessary, to represent the value before nanosecond resolution. Note: this format may exceed the ISO 8601 limit in milliseconds (3 decimal places).
Code example
Here is sample code in Java 8 Update 51.
String output = Instant.parse( "2015-08-20T08:26:21.000Z" ).toString( ); System.out.println("output: " + output );
: 2015-08-20T08: 26: 21Z
Transition to a fractional second .08
String output = Instant.parse( "2015-08-20T08:26:21.08Z" ).toString( );
: 2015-08-20T08: 26: 21.080Z
If you are interested in any time zone other than UTC, then create a ZonedDateTime object from this Instant .
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , ZoneId.of( "America/Montreal" ) ) ;