Java 8 date-time parsing with optional 2-4 year components [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
Java 8 DateTimeFormatter two digit year 18 parsed to 0018 instead of 2018?
2 answers
I would like to parse LocalDate objects from various date strings. All strings already have the format dd-MM-yy or dd-MM-yyyy. In the first case the base year should be prepended with 19 instead of 20.
I have this code but I can't get it working. What am I doing wrong?
public static LocalDate parseDateString(String date) {
DateTimeFormatter optionalFullYearParser = java.time.format.DateTimeFormatter.ofPattern("[dd-MM-yy][dd-MM-yyyy]");
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.appendOptional(optionalFullYearParser)
.appendValueReduced(ChronoField.YEAR, 2, 4, 1900)
.toFormatter(new Locale ("nl","NL"));
return java.time.LocalDate.parse(date,parser);
}
System.out.println(parseDateString("11-10-56").toString());
System.out.println(parseDateString("11-10-1956").toString());
System.out.println(parseDateString("08-05-51").toString());
When I run this code it throws an Exception:
java.time.format.DateTimeParseException: Text '11-10-56' could not be parsed at index 8
...
Any help will be greatly appreciated!
java datetime java-8 java-time
marked as duplicate by Ole V.V.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 22:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
Java 8 DateTimeFormatter two digit year 18 parsed to 0018 instead of 2018?
2 answers
I would like to parse LocalDate objects from various date strings. All strings already have the format dd-MM-yy or dd-MM-yyyy. In the first case the base year should be prepended with 19 instead of 20.
I have this code but I can't get it working. What am I doing wrong?
public static LocalDate parseDateString(String date) {
DateTimeFormatter optionalFullYearParser = java.time.format.DateTimeFormatter.ofPattern("[dd-MM-yy][dd-MM-yyyy]");
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.appendOptional(optionalFullYearParser)
.appendValueReduced(ChronoField.YEAR, 2, 4, 1900)
.toFormatter(new Locale ("nl","NL"));
return java.time.LocalDate.parse(date,parser);
}
System.out.println(parseDateString("11-10-56").toString());
System.out.println(parseDateString("11-10-1956").toString());
System.out.println(parseDateString("08-05-51").toString());
When I run this code it throws an Exception:
java.time.format.DateTimeParseException: Text '11-10-56' could not be parsed at index 8
...
Any help will be greatly appreciated!
java datetime java-8 java-time
marked as duplicate by Ole V.V.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 22:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
You’re over-complicating it. See how simple it is in my answer to the linked original question. The trick is to haveyyyy
beforeyy
, or it will parse the first two of the four digits as a two digit year and then get stuck.
– Ole V.V.
Nov 21 at 22:11
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
Java 8 DateTimeFormatter two digit year 18 parsed to 0018 instead of 2018?
2 answers
I would like to parse LocalDate objects from various date strings. All strings already have the format dd-MM-yy or dd-MM-yyyy. In the first case the base year should be prepended with 19 instead of 20.
I have this code but I can't get it working. What am I doing wrong?
public static LocalDate parseDateString(String date) {
DateTimeFormatter optionalFullYearParser = java.time.format.DateTimeFormatter.ofPattern("[dd-MM-yy][dd-MM-yyyy]");
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.appendOptional(optionalFullYearParser)
.appendValueReduced(ChronoField.YEAR, 2, 4, 1900)
.toFormatter(new Locale ("nl","NL"));
return java.time.LocalDate.parse(date,parser);
}
System.out.println(parseDateString("11-10-56").toString());
System.out.println(parseDateString("11-10-1956").toString());
System.out.println(parseDateString("08-05-51").toString());
When I run this code it throws an Exception:
java.time.format.DateTimeParseException: Text '11-10-56' could not be parsed at index 8
...
Any help will be greatly appreciated!
java datetime java-8 java-time
This question already has an answer here:
Java 8 DateTimeFormatter two digit year 18 parsed to 0018 instead of 2018?
2 answers
I would like to parse LocalDate objects from various date strings. All strings already have the format dd-MM-yy or dd-MM-yyyy. In the first case the base year should be prepended with 19 instead of 20.
I have this code but I can't get it working. What am I doing wrong?
public static LocalDate parseDateString(String date) {
DateTimeFormatter optionalFullYearParser = java.time.format.DateTimeFormatter.ofPattern("[dd-MM-yy][dd-MM-yyyy]");
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.appendOptional(optionalFullYearParser)
.appendValueReduced(ChronoField.YEAR, 2, 4, 1900)
.toFormatter(new Locale ("nl","NL"));
return java.time.LocalDate.parse(date,parser);
}
System.out.println(parseDateString("11-10-56").toString());
System.out.println(parseDateString("11-10-1956").toString());
System.out.println(parseDateString("08-05-51").toString());
When I run this code it throws an Exception:
java.time.format.DateTimeParseException: Text '11-10-56' could not be parsed at index 8
...
Any help will be greatly appreciated!
This question already has an answer here:
Java 8 DateTimeFormatter two digit year 18 parsed to 0018 instead of 2018?
2 answers
java datetime java-8 java-time
java datetime java-8 java-time
asked Nov 21 at 21:18
Ferry Kranenburg
1,6871019
1,6871019
marked as duplicate by Ole V.V.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 22:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Ole V.V.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 22:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
You’re over-complicating it. See how simple it is in my answer to the linked original question. The trick is to haveyyyy
beforeyy
, or it will parse the first two of the four digits as a two digit year and then get stuck.
– Ole V.V.
Nov 21 at 22:11
add a comment |
1
You’re over-complicating it. See how simple it is in my answer to the linked original question. The trick is to haveyyyy
beforeyy
, or it will parse the first two of the four digits as a two digit year and then get stuck.
– Ole V.V.
Nov 21 at 22:11
1
1
You’re over-complicating it. See how simple it is in my answer to the linked original question. The trick is to have
yyyy
before yy
, or it will parse the first two of the four digits as a two digit year and then get stuck.– Ole V.V.
Nov 21 at 22:11
You’re over-complicating it. See how simple it is in my answer to the linked original question. The trick is to have
yyyy
before yy
, or it will parse the first two of the four digits as a two digit year and then get stuck.– Ole V.V.
Nov 21 at 22:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
There is no need for 2 formatter objects to do this, and you shouldn't include the year part in the pattern text when you then use appendValueReduced
to add the year part.
public static LocalDate parseDateString(String date) {
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.appendPattern("dd-MM-")
.appendValueReduced(ChronoField.YEAR, 2, 4, 1900)
.toFormatter(Locale.forLanguageTag("nl-NL"));
return java.time.LocalDate.parse(date, parser);
}
Test
System.out.println(parseDateString("11-10-56").toString());
System.out.println(parseDateString("11-10-1956").toString());
System.out.println(parseDateString("08-05-51").toString());
Output
1956-10-11
1956-10-11
1951-05-08
Thank you very much, this works!
– Ferry Kranenburg
Nov 21 at 21:49
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
There is no need for 2 formatter objects to do this, and you shouldn't include the year part in the pattern text when you then use appendValueReduced
to add the year part.
public static LocalDate parseDateString(String date) {
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.appendPattern("dd-MM-")
.appendValueReduced(ChronoField.YEAR, 2, 4, 1900)
.toFormatter(Locale.forLanguageTag("nl-NL"));
return java.time.LocalDate.parse(date, parser);
}
Test
System.out.println(parseDateString("11-10-56").toString());
System.out.println(parseDateString("11-10-1956").toString());
System.out.println(parseDateString("08-05-51").toString());
Output
1956-10-11
1956-10-11
1951-05-08
Thank you very much, this works!
– Ferry Kranenburg
Nov 21 at 21:49
add a comment |
up vote
2
down vote
accepted
There is no need for 2 formatter objects to do this, and you shouldn't include the year part in the pattern text when you then use appendValueReduced
to add the year part.
public static LocalDate parseDateString(String date) {
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.appendPattern("dd-MM-")
.appendValueReduced(ChronoField.YEAR, 2, 4, 1900)
.toFormatter(Locale.forLanguageTag("nl-NL"));
return java.time.LocalDate.parse(date, parser);
}
Test
System.out.println(parseDateString("11-10-56").toString());
System.out.println(parseDateString("11-10-1956").toString());
System.out.println(parseDateString("08-05-51").toString());
Output
1956-10-11
1956-10-11
1951-05-08
Thank you very much, this works!
– Ferry Kranenburg
Nov 21 at 21:49
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
There is no need for 2 formatter objects to do this, and you shouldn't include the year part in the pattern text when you then use appendValueReduced
to add the year part.
public static LocalDate parseDateString(String date) {
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.appendPattern("dd-MM-")
.appendValueReduced(ChronoField.YEAR, 2, 4, 1900)
.toFormatter(Locale.forLanguageTag("nl-NL"));
return java.time.LocalDate.parse(date, parser);
}
Test
System.out.println(parseDateString("11-10-56").toString());
System.out.println(parseDateString("11-10-1956").toString());
System.out.println(parseDateString("08-05-51").toString());
Output
1956-10-11
1956-10-11
1951-05-08
There is no need for 2 formatter objects to do this, and you shouldn't include the year part in the pattern text when you then use appendValueReduced
to add the year part.
public static LocalDate parseDateString(String date) {
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.appendPattern("dd-MM-")
.appendValueReduced(ChronoField.YEAR, 2, 4, 1900)
.toFormatter(Locale.forLanguageTag("nl-NL"));
return java.time.LocalDate.parse(date, parser);
}
Test
System.out.println(parseDateString("11-10-56").toString());
System.out.println(parseDateString("11-10-1956").toString());
System.out.println(parseDateString("08-05-51").toString());
Output
1956-10-11
1956-10-11
1951-05-08
edited Nov 21 at 21:38
NiVeR
6,68241930
6,68241930
answered Nov 21 at 21:32
Andreas
73.4k456118
73.4k456118
Thank you very much, this works!
– Ferry Kranenburg
Nov 21 at 21:49
add a comment |
Thank you very much, this works!
– Ferry Kranenburg
Nov 21 at 21:49
Thank you very much, this works!
– Ferry Kranenburg
Nov 21 at 21:49
Thank you very much, this works!
– Ferry Kranenburg
Nov 21 at 21:49
add a comment |
1
You’re over-complicating it. See how simple it is in my answer to the linked original question. The trick is to have
yyyy
beforeyy
, or it will parse the first two of the four digits as a two digit year and then get stuck.– Ole V.V.
Nov 21 at 22:11