Am using SimpleDateFormat to format or validate the dates, but I would like to make it thread-safe by using java 8 DateTimeFormatter. I am having trouble to achieve some requirement.
My application will accept only three types of formats. "yyyy-MM-dd", "yyyy-MM", "yyyy"
Existing Code gives me desired output:
/*simple date format to process yyyy-MM-dd format
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd")
/*simple date format to process yyyy-MM format
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM")
/*simple date format to process yyyy format
SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy")
/* to parse input
simpleDateFormat.parse(input)
/* to format
simpleDateFormat.format(simpleDateFormat1)
Here is the input and expected output:
input expected
'2018-03-19' '2018-03-19'
'2018-03' '2018-03'
'2018' '2018'
'2017-02-54' '2017-02'
'2016-13-19' '2016'
How can I achieve same result in java 8 DateTimeForma
enter code here
tter?/* java 8 date time formatter DateTimeFormatter dateTimeFormatter = new DateTimeFormatter("yyyy-MM-dd")
The above snippet works when all year and month and date values are correct. Any help would be highly appreciated.