Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make Joda work with String.format method (i.e. java.util.Formatter class)?
    primarykey
    data
    text
    <p>Is it possible to make an <code>org.joda.time.LocalDate.LocalDate</code> object work seamlessly with <code>String.format</code>? Here is a simple test case to illustrate what I'm trying to do:</p> <pre><code>public class JodaTests { @Test public void testLocalDateStringFormat() { final Logger logger = LoggerFactory.getLogger(JodaTests.class); //works, prints: 2012-06-01 logger.info(String.format("%s", new LocalDate())); //works, prints: 06/01/2012 logger.info(String.format("%1$tm/%1$td/%1$tY", new LocalDate().toDate())); //throws java.util.IllegalFormatConversionException: m != org.joda.time.LocalDate logger.info(String.format("%1$tm/%1$td/%1$tY", new LocalDate())); } } </code></pre> <p>So clearly, I can call the <code>toDate</code> method to convert the <code>LocalDate</code> to a <code>java.util.Date</code>, which then works fine with <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html" rel="nofollow">Formatter</a>. But is there a way to configure Joda and/or the JDK somehow to make the 3rd line I have there (the one that throws the exception) work? I have tried web searches, and also searched a bit through the Joda API but didn't see any obvious answer.</p> <p><strong>Clarification</strong>: I know about Joda's built-in formatting capabilities (via <code>DateTimeFormat</code>) but I specifically need to format a <code>LocalDate</code> with <code>String.format</code> due to some library code I'm using.</p> <p>It looks like there is some limited capability to do this by implementing the <code>Formattable</code> interface in your custom type as described in <a href="https://blogs.oracle.com/CoreJavaTechTips/entry/using_printf_with_customized_formattable" rel="nofollow">this article</a>. Unfortunately, <code>LocalDate</code> is a <code>final</code> class so I can't (easily) extend it to implement the interface. I guess a wrapper class is the only way to go. Or, of course, by using <code>%d</code> format strings then fetching each individual field (i.e. <code>LocalDate.getYear</code>), which isn't desirable in my case.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload