DateTimeFormatter to serialize LocalDate as ordinal 1st/2nd/3rd of a month [duplicate]
This question already has an answer here:
Printing date in java with ordinal characters [duplicate]
1 answer
I'd like to display LocalDate
as:
first day: 1st;
second day: 2nd;
third day: 3rd;
all rest days: Nth.
e.g. 1980-10-1
as 1st Oct 1980
I'm able to serialize it excluding the first 3 days using DateTimeFormatter.ofPattern("dth MMM yyyy")
.
Since the first 3 days have a different pattern than the rest days, how to construct the formatter to serialize also the first 3 days?
java
marked as duplicate by Basil Bourque
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 20 '18 at 18:12
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 |
This question already has an answer here:
Printing date in java with ordinal characters [duplicate]
1 answer
I'd like to display LocalDate
as:
first day: 1st;
second day: 2nd;
third day: 3rd;
all rest days: Nth.
e.g. 1980-10-1
as 1st Oct 1980
I'm able to serialize it excluding the first 3 days using DateTimeFormatter.ofPattern("dth MMM yyyy")
.
Since the first 3 days have a different pattern than the rest days, how to construct the formatter to serialize also the first 3 days?
java
marked as duplicate by Basil Bourque
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 20 '18 at 18:12
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.
Also, be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
See stackoverflow.com/questions/26337836/…
– Krease
Nov 20 '18 at 17:47
@Krease this topic is J7 or Older
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 18:35
here is another good answer similar to the one below
– Krease
Nov 20 '18 at 18:49
add a comment |
This question already has an answer here:
Printing date in java with ordinal characters [duplicate]
1 answer
I'd like to display LocalDate
as:
first day: 1st;
second day: 2nd;
third day: 3rd;
all rest days: Nth.
e.g. 1980-10-1
as 1st Oct 1980
I'm able to serialize it excluding the first 3 days using DateTimeFormatter.ofPattern("dth MMM yyyy")
.
Since the first 3 days have a different pattern than the rest days, how to construct the formatter to serialize also the first 3 days?
java
This question already has an answer here:
Printing date in java with ordinal characters [duplicate]
1 answer
I'd like to display LocalDate
as:
first day: 1st;
second day: 2nd;
third day: 3rd;
all rest days: Nth.
e.g. 1980-10-1
as 1st Oct 1980
I'm able to serialize it excluding the first 3 days using DateTimeFormatter.ofPattern("dth MMM yyyy")
.
Since the first 3 days have a different pattern than the rest days, how to construct the formatter to serialize also the first 3 days?
This question already has an answer here:
Printing date in java with ordinal characters [duplicate]
1 answer
java
java
edited Nov 20 '18 at 18:11
Basil Bourque
108k26372537
108k26372537
asked Nov 20 '18 at 16:43
LunaticJapeLunaticJape
13211
13211
marked as duplicate by Basil Bourque
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 20 '18 at 18:12
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 Basil Bourque
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 20 '18 at 18:12
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.
Also, be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
See stackoverflow.com/questions/26337836/…
– Krease
Nov 20 '18 at 17:47
@Krease this topic is J7 or Older
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 18:35
here is another good answer similar to the one below
– Krease
Nov 20 '18 at 18:49
add a comment |
Also, be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
See stackoverflow.com/questions/26337836/…
– Krease
Nov 20 '18 at 17:47
@Krease this topic is J7 or Older
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 18:35
here is another good answer similar to the one below
– Krease
Nov 20 '18 at 18:49
Also, be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
Also, be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
See stackoverflow.com/questions/26337836/…
– Krease
Nov 20 '18 at 17:47
See stackoverflow.com/questions/26337836/…
– Krease
Nov 20 '18 at 17:47
@Krease this topic is J7 or Older
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 18:35
@Krease this topic is J7 or Older
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 18:35
here is another good answer similar to the one below
– Krease
Nov 20 '18 at 18:49
here is another good answer similar to the one below
– Krease
Nov 20 '18 at 18:49
add a comment |
1 Answer
1
active
oldest
votes
here is what you are searching : https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html#appendText-java.time.temporal.TemporalField-java.util.Map-
and here is an exemple :
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
Map<Long, String> mapToRoman = new HashMap<>();
mapToRoman.put(1L, "1st");
mapToRoman.put(2L, "2nd");
mapToRoman.put(3L, "3rd");
mapToRoman.put(4L, "4th");
// continue to map all available days in a month
builder.appendText(ChronoField.DAY_OF_MONTH, mapToRoman );
builder.append(DateTimeFormatter.ofPattern(" MM yyyy", Locale.US));
DateTimeFormatter formatter = builder.toFormatter();
1
be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
yes of course this is just a sample^^ even the "th" isn't present here
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 17:54
1
This is a really good answer. A little fix: One M is missing in the month part: builder.append(DateTimeFormatter.ofPattern(" MMM yyyy", Locale.US));
– Adrian M. Paredes
Nov 20 '18 at 18:22
Is there a simpler way to just add 4 patterns (since it has only 4 different patterns) instead of adding all of 31 days?
– LunaticJape
Nov 20 '18 at 18:53
@LunaticJape - Sort of - a similar answer here uses the same logic, but puts coverage for all 31 days in a few lines
– Krease
Nov 20 '18 at 23:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
here is what you are searching : https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html#appendText-java.time.temporal.TemporalField-java.util.Map-
and here is an exemple :
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
Map<Long, String> mapToRoman = new HashMap<>();
mapToRoman.put(1L, "1st");
mapToRoman.put(2L, "2nd");
mapToRoman.put(3L, "3rd");
mapToRoman.put(4L, "4th");
// continue to map all available days in a month
builder.appendText(ChronoField.DAY_OF_MONTH, mapToRoman );
builder.append(DateTimeFormatter.ofPattern(" MM yyyy", Locale.US));
DateTimeFormatter formatter = builder.toFormatter();
1
be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
yes of course this is just a sample^^ even the "th" isn't present here
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 17:54
1
This is a really good answer. A little fix: One M is missing in the month part: builder.append(DateTimeFormatter.ofPattern(" MMM yyyy", Locale.US));
– Adrian M. Paredes
Nov 20 '18 at 18:22
Is there a simpler way to just add 4 patterns (since it has only 4 different patterns) instead of adding all of 31 days?
– LunaticJape
Nov 20 '18 at 18:53
@LunaticJape - Sort of - a similar answer here uses the same logic, but puts coverage for all 31 days in a few lines
– Krease
Nov 20 '18 at 23:18
add a comment |
here is what you are searching : https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html#appendText-java.time.temporal.TemporalField-java.util.Map-
and here is an exemple :
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
Map<Long, String> mapToRoman = new HashMap<>();
mapToRoman.put(1L, "1st");
mapToRoman.put(2L, "2nd");
mapToRoman.put(3L, "3rd");
mapToRoman.put(4L, "4th");
// continue to map all available days in a month
builder.appendText(ChronoField.DAY_OF_MONTH, mapToRoman );
builder.append(DateTimeFormatter.ofPattern(" MM yyyy", Locale.US));
DateTimeFormatter formatter = builder.toFormatter();
1
be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
yes of course this is just a sample^^ even the "th" isn't present here
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 17:54
1
This is a really good answer. A little fix: One M is missing in the month part: builder.append(DateTimeFormatter.ofPattern(" MMM yyyy", Locale.US));
– Adrian M. Paredes
Nov 20 '18 at 18:22
Is there a simpler way to just add 4 patterns (since it has only 4 different patterns) instead of adding all of 31 days?
– LunaticJape
Nov 20 '18 at 18:53
@LunaticJape - Sort of - a similar answer here uses the same logic, but puts coverage for all 31 days in a few lines
– Krease
Nov 20 '18 at 23:18
add a comment |
here is what you are searching : https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html#appendText-java.time.temporal.TemporalField-java.util.Map-
and here is an exemple :
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
Map<Long, String> mapToRoman = new HashMap<>();
mapToRoman.put(1L, "1st");
mapToRoman.put(2L, "2nd");
mapToRoman.put(3L, "3rd");
mapToRoman.put(4L, "4th");
// continue to map all available days in a month
builder.appendText(ChronoField.DAY_OF_MONTH, mapToRoman );
builder.append(DateTimeFormatter.ofPattern(" MM yyyy", Locale.US));
DateTimeFormatter formatter = builder.toFormatter();
here is what you are searching : https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html#appendText-java.time.temporal.TemporalField-java.util.Map-
and here is an exemple :
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
Map<Long, String> mapToRoman = new HashMap<>();
mapToRoman.put(1L, "1st");
mapToRoman.put(2L, "2nd");
mapToRoman.put(3L, "3rd");
mapToRoman.put(4L, "4th");
// continue to map all available days in a month
builder.appendText(ChronoField.DAY_OF_MONTH, mapToRoman );
builder.append(DateTimeFormatter.ofPattern(" MM yyyy", Locale.US));
DateTimeFormatter formatter = builder.toFormatter();
edited Nov 20 '18 at 17:55
answered Nov 20 '18 at 17:39
Arnault Le Prévost-CorvellecArnault Le Prévost-Corvellec
3978
3978
1
be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
yes of course this is just a sample^^ even the "th" isn't present here
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 17:54
1
This is a really good answer. A little fix: One M is missing in the month part: builder.append(DateTimeFormatter.ofPattern(" MMM yyyy", Locale.US));
– Adrian M. Paredes
Nov 20 '18 at 18:22
Is there a simpler way to just add 4 patterns (since it has only 4 different patterns) instead of adding all of 31 days?
– LunaticJape
Nov 20 '18 at 18:53
@LunaticJape - Sort of - a similar answer here uses the same logic, but puts coverage for all 31 days in a few lines
– Krease
Nov 20 '18 at 23:18
add a comment |
1
be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
yes of course this is just a sample^^ even the "th" isn't present here
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 17:54
1
This is a really good answer. A little fix: One M is missing in the month part: builder.append(DateTimeFormatter.ofPattern(" MMM yyyy", Locale.US));
– Adrian M. Paredes
Nov 20 '18 at 18:22
Is there a simpler way to just add 4 patterns (since it has only 4 different patterns) instead of adding all of 31 days?
– LunaticJape
Nov 20 '18 at 18:53
@LunaticJape - Sort of - a similar answer here uses the same logic, but puts coverage for all 31 days in a few lines
– Krease
Nov 20 '18 at 23:18
1
1
be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
yes of course this is just a sample^^ even the "th" isn't present here
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 17:54
yes of course this is just a sample^^ even the "th" isn't present here
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 17:54
1
1
This is a really good answer. A little fix: One M is missing in the month part: builder.append(DateTimeFormatter.ofPattern(" MMM yyyy", Locale.US));
– Adrian M. Paredes
Nov 20 '18 at 18:22
This is a really good answer. A little fix: One M is missing in the month part: builder.append(DateTimeFormatter.ofPattern(" MMM yyyy", Locale.US));
– Adrian M. Paredes
Nov 20 '18 at 18:22
Is there a simpler way to just add 4 patterns (since it has only 4 different patterns) instead of adding all of 31 days?
– LunaticJape
Nov 20 '18 at 18:53
Is there a simpler way to just add 4 patterns (since it has only 4 different patterns) instead of adding all of 31 days?
– LunaticJape
Nov 20 '18 at 18:53
@LunaticJape - Sort of - a similar answer here uses the same logic, but puts coverage for all 31 days in a few lines
– Krease
Nov 20 '18 at 23:18
@LunaticJape - Sort of - a similar answer here uses the same logic, but puts coverage for all 31 days in a few lines
– Krease
Nov 20 '18 at 23:18
add a comment |
Also, be careful to account for "21st", "22nd", "23rd", and "31st" that don't follow the "th" pattern
– Krease
Nov 20 '18 at 17:44
See stackoverflow.com/questions/26337836/…
– Krease
Nov 20 '18 at 17:47
@Krease this topic is J7 or Older
– Arnault Le Prévost-Corvellec
Nov 20 '18 at 18:35
here is another good answer similar to the one below
– Krease
Nov 20 '18 at 18:49