DateTimeFormatter to serialize LocalDate as ordinal 1st/2nd/3rd of a month [duplicate]












3
















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?










share|improve this question















marked as duplicate by Basil Bourque java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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
















3
















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?










share|improve this question















marked as duplicate by Basil Bourque java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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














3












3








3









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?










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:11









Basil Bourque

107k25367531




107k25367531










asked Nov 20 '18 at 16:43









LunaticJapeLunaticJape

12511




12511




marked as duplicate by Basil Bourque java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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



















  • 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












1 Answer
1






active

oldest

votes


















2














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();





share|improve this answer





















  • 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 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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();





share|improve this answer





















  • 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


















2














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();





share|improve this answer





















  • 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
















2












2








2







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();





share|improve this answer















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();






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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





Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?