ISO8601DateParser { public static Date parse( String input ) { Matcher m = PATTERN.matcher( input ); if ( m.find() ) { try { String zone = m.group( 9 ); String javazone = zone; if ( "Z".equals( zone ) || zone == null ) { javazone = "UTC"; } else if ( zone.startsWith( "+" ) || zone.startsWith( "-" ) ) { javazone = "GMT" + zone; } else { throw new RuntimeException( "Unknown timezone: " + zone ); } TimeZone tz = null; Calendar c = null; long offset = 0; try { tz = TimeZoneCache.getTimeZone( javazone ); c = Calendar.getInstance( tz ); } catch ( Exception e ) { tz = TimeZoneCache.getTimeZone( "UTC" ); offset = computeTimezoneOffset( zone ); c = Calendar.getInstance( tz ); } int year = toInt( m.group( 1 ) ); c.set( Calendar.YEAR, year ); c.set( Calendar.MONTH, toInt( m.group( 2 ) ) - 1 ); c.set( Calendar.DAY_OF_MONTH, toInt( m.group( 3 ) ) ); c.set( Calendar.HOUR_OF_DAY, toInt( m.group( 4 ) ) ); c.set( Calendar.MINUTE, toInt( m.group( 5 ) ) ); int second = toInt( m.group( 7 ) ); if ( second == -1 ) second = 0; c.set( Calendar.SECOND, second ); c.set( Calendar.MILLISECOND, 0 ); Date result = c.getTime(); result = new Date( result.getTime() + offset ); return result; } catch ( RuntimeException e ) { throw new RuntimeException( "Could not parse ISO 8601 timestamp: " + input, e ); } } return null; }  static Date parse( String input ); static int toInt( String v ); static int toInt( String v, int _default ); static String toString( Date date );  }
@Test @Ignore public void testParseWithJava8() throws Exception { String strDatewithTime = "2016-02-27 05:28:16 -0500"; LocalDateTime aLDT = LocalDateTime.parse(strDatewithTime); System.out.println("Date with Time: " + aLDT); }
@Test public void testDateParseWithJava8UsingPartials() throws Exception { String date = "2016-03-02"; LocalDate aLD = LocalDate.parse(date); System.out.println("Date: " + aLD); }