Breaking a date pattern into its atomic elements

J

Jacques Vidal

Question:

If you apply the "MM/dd/yyyy" pattern to, eg, a SimpleDateFormat
instance, isn't there a standard way to query this instance to
retrieve an array or Collection of its atomic elements? In this case,
{ "MM", "/", "dd", "/", "yyyy" }? Or even just the formatting
elements, { "MM", "dd", "yyyy" }?

I'd thought this feature would be available, anyway so far the only
thing I've seen that could help splitting the pattern string into
elements is the data stored in the runStarts field of the
AttributedString class - if you create an instance of this class using
formatToCharacterIterator() on yout SimpleDateFormat instance -, but
it turns out this field isn't visible outside of its enclosing
package.

Is there a way I've missed to get what I want? I just can't believe
I'll have to code a dedicated parser to do that.
 
B

Bryce

If you apply the "MM/dd/yyyy" pattern to, eg, a SimpleDateFormat
instance, isn't there a standard way to query this instance to
retrieve an array or Collection of its atomic elements? In this case,
{ "MM", "/", "dd", "/", "yyyy" }? Or even just the formatting
elements, { "MM", "dd", "yyyy" }?

Two methods:

1. Use GregorianCalendar, who's interface Calendar has some methods.
GregorianCalendar calendar = new GregorianCalendar(2004, 05, 05);
System.out.println("Year String: " + calendar.get(Calendar.YEAR);
System.out.println("Month String: " + calendar.get(Calendar.MONTH);
System.out.println("Day String: " +
calendar.get(Calendar.DAY_OF_MONTH);

2. If you can guarantee the pattern will be the same, the use
String.split function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top