tok tok - 1 year ago 91
Java Question

Unparseable date with extra number in Java

What's wrong in this code? I'm trying to parse a date format that has 0 between years and months.

import java.text.SimpleDateFormat;

class Main {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy'0'MMdd");
try {
Date date = format.parse("201600101");
} catch (Exception ex) {

This outputs Unparseable date: "201600101". If I change '0' to anything but number [e.g. 'X' and format.parse("2016X0101")] this will work.

Answer Source

As Peter Lawrey said Java sees '20160' as a year. You can solve your problem by modifying "201600101" to, for example, 2016-00101 and refactor your format patter as

SimpleDateFormat format = new SimpleDateFormat("yyyy-'0'MMdd");

That will parse your date.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download