I wanted to convert date in yymmdd format to YYYYMMDD, but when using the simpledateformat class i am getting the years after 1970, but the requirement is of the year which is prior to 1970.
-
show what you mean in code? – Eugene Jul 11 '19 at 12:20
-
2I recommend you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 11 '19 at 15:18
-
1I believe that there are a couple of helpful answers here: [How to convert two digit year to full year using Java 8 time API](https://stackoverflow.com/questions/50719918/how-to-convert-two-digit-year-to-full-year-using-java-8-time-api) (also when the question isn’t exactly the same). – Ole V.V. Jul 11 '19 at 15:21
-
1Use Java 8 java.time api, older date api is broken. – Jean-Baptiste Yunès Jul 11 '19 at 15:38
-
1Besides using an outdated, discouraged API, is it really that hard to find the `set2DigitYearStart` method in it? – Holger Jul 11 '19 at 16:01
-
2Welcome to Stack Overflow. I’ll be brutally honest with you, in my view this is a poor question by Stack Overflow standards. It shows no search, research or other effort, it has no code, no minimal example, no example input and output. I know you’re new here, so I have provided an answer anyway. It takes a bit to learn [how to ask a good question](https://stackoverflow.com/help/how-to-ask). Please make an effort, and I trust you’ll learn. – Ole V.V. Jul 11 '19 at 17:57
1 Answers
java.time
Parsing input
The way to control the interpretation of the 2-digit year in yymmdd
is through the appendValueReduced
method of DateTimeFormatterBuilder
.
DateTimeFormatter twoDigitFormatter = new DateTimeFormatterBuilder()
.appendValueReduced(ChronoField.YEAR, 2, 2, 1870)
.appendPattern("MMdd")
.toFormatter();
String exampleInput = "691129";
LocalDate date = LocalDate.parse(exampleInput, twoDigitFormatter);
Supplying a base year of 1870 causes two-digit years to be interpreted in the range 1870 through 1969 (so always prior to 1970). Supply a different base year according to your requirements. Also unless you are sure that input years all across a period of 100 years are expected and valid, I recommend you do a range check of the parsed date.
Formatting and printing output
DateTimeFormatter fourDigitFormatter = DateTimeFormatter.ofPattern("uuuuMMdd");
String result = date.format(fourDigitFormatter);
System.out.println(result);
Output in this example is:
19691129
If instead input was 700114
, output is:
18700114
Use LocalDate for keeping your date
Instead of converting your date from one string format to another, I suggest that it’s better to keep your date in a LocalDate
, not a string (just like you don’t keep an integer value in a string). When your program accepts string input, parse into a LocalDate
at once. Only when it needs to give string output, format the LocalDate
back into a string. For this reason I have also separated parsing from formatting above.
Link
Oracle tutorial: Date Time explaining how to use java.time.

- 81,772
- 15
- 137
- 161