开发者

How to parse two different type of string formats to a date?

I am trying to format a date like these

august 2011, and august 24,2011

I am using this to parse them now..

//set the pattern here to look like the pattern you are expecting in your 'Date' variable
                    SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd,yyyy");

 //Get the release date
                     releaseDate = postIt.next().text();

                     Date realDate = null;
                    try {
                        realDate = sdf.parse(releaseDate);
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 

                     int i = 0;
                    String gameDate =  realDate.toString();

i get the following error in my debug.

08-26 22:02:44.571: WARN/System.err(29218): java.text.ParseException: Unparseable date: "August 2011" (at offset 11)
08-26 22:02:44.571: WARN/System.err(29218)开发者_StackOverflow:     at java.text.DateFormat.parse(DateFormat.java:626)

How would i go about making this to where it will read both formats?

EDIT:

This is how it parses it now...

08-27 00:10:48.951: VERBOSE/Dates(30449): August 2011  

If formated the August 2011 to

08-27 00:10:48.951: ERROR/FormattedDATE(30449): Mon Aug 01 00:00:00 EDT 2011

They all parse to this same date.


I think you'll need a format string for each e.g.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatDriver {
    public static void main( String[] args ) throws Exception {

        SimpleDateFormat dayMonthYearFormatter = new SimpleDateFormat("MMMM dd,yyyy");
        SimpleDateFormat monthYearFormatter = new SimpleDateFormat("MMMM yyyy");                

        //String date1 = "August 11,2005";
        String date1 = "August 2005";
        Date realDate = null;

        try {
            realDate = dayMonthYearFormatter.parse(date1);
            System.out.println("parsed date -> " + realDate);
        } catch (ParseException e) {                    
            // fall back on other formatter
            try {               
                realDate = monthYearFormatter.parse(date1);
                System.out.println("parsed date -> " + realDate);
            } catch(ParseException e2) {
                System.err.println("could not parse" + date1);              
            }
        }                          
    }        
}

I would recommend you check out http://joda-time.sourceforge.net

This is just an idea, not sure if I'd really use it but a tokenizer could be used to see what date type it is, 2 tokens for 'August 11, 2005', 1 for 'August 2005'. So use one formatter if token count is 1, other formatter if token count is 2 e.g.

    int tokenCount = 0;
    StringTokenizer tokenizer = new StringTokenizer("August 11, 2005", ",");
    while(tokenizer.hasMoreTokens()) {
        tokenizer.nextToken();
        tokenCount++;
    }
    System.out.println("token count -> " + tokenCount);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜