If the +0
prefix is a constant, then you can just quote it using singlequotes.
String string = "+020110312";
Date date = new SimpleDateFormat("'+0'yyyyMMdd").parse(string);
System.out.println(date); // Sat Mar 12 00:00:00 GMT-04:00 2011
(outcome is correct as per my timezone)
Note that months are represented by MM
not mm
. The mm
represents minutes. Using mm
for months would corrupt the parsing outcome.
See also:
Update: since that seem to vary in the future "but in future it may be more than one zeros", you can better consider to substring the last 8 characters.
String string = "+020110312";
Date date = new SimpleDateFormat("yyyyMMdd").parse(string.substring(string.length() - 8));
System.out.println(date); // Sat Mar 12 00:00:00 GMT-04:00 2011