Undiluted date, with additional numbers in Java

What's wrong with this code? I try to parse the date format between months 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");
            System.out.println(date);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

This will output the undiluted date: "201600101" If I change "0" to a number ['x' and format. Parse ("2016x0101")] this will work

Solution

As Peter Lawley said, Java believes that "2016" is a year You can solve the problem by modifying "201600101" to, for example, 2016-00101, and refactoring the format pattern to

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

This will parse your date

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>