开发者

Check date format before parsing

开发者 https://www.devze.com 2023-01-14 11:59 出处:网络
I am parsing several documments with the field Duration. But in the differents files, it is in differnt formats, ex:

I am parsing several documments with the field Duration. But in the differents files, it is in differnt formats, ex:

"Duration": "00:43"  
"Duration": "113.046"  
"Duration": "21.55 s"

I want to parse all of them 开发者_StackOverflow社区to the format "Duration": "113.046", how could I check before any parsing in wich format it is??

Some conditions before this piece of code, because this is not right for all of them:

Long duration;
                    DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
                    try {
                        Date durationD = sdf.parse(totalDuration);
                        Date zeroSec = sdf.parse("00:00:00");
                        duration = durationD.getTime() - zeroSec.getTime();
                    } catch (Exception e) {                     
                            duration = Long.parseLong(totalDuration);                       
                    }

Thanks in advance


You could match the pattern with help of regex and then format accordingly. Here's a kickoff example:

Map<Pattern, DateFormat> dateFormatPatterns = new HashMap<Pattern, DateFormat>();
dateFormatPatterns.put(Pattern.compile("\\d{1,2}:\\d{2}"), new SimpleDateFormat("H:m"));
dateFormatPatterns.put(Pattern.compile("\\d{1,3}\\.\\d{3}"), new SimpleDateFormat("s.S"));
dateFormatPatterns.put(Pattern.compile("\\d{1,2}\\.\\d{2} s"), new SimpleDateFormat("s.S 's'"));

String[] strings = { "00:43", "113.046", "21.55 s" };
DateFormat finalFormat = new SimpleDateFormat("HH:mm:ss");

for (String string : strings) {
    for (Pattern pattern : dateFormatPatterns.keySet()) {
        if (pattern.matcher(string).matches()) {
            Date date = dateFormatPatterns.get(pattern).parse(string);
            String formattedTime = finalFormat.format(date);
            System.out.println(formattedTime);
            break;
        }
    }
}

This yields here

00:43:00
00:01:53
00:00:21


If these are all your known input formats, then convert your input to your expected date format.

Just string-replace all : with . and remove s.


Do not forget to strip the spaces, too. By the way, "113.046" seems a bit odd date format to me - if I were in your shoes, I would have used some of the standard date time formats and convert the irregular ones.


My solution, not smart at all:

long DurationFixer(String duration){
        long durationLong = 0;

        if(duration.contains(":")){
            DateFormat sdf = new SimpleDateFormat("mm:ss");
            try {
                Date durationD = sdf.parse(duration);
                Date zeroSec = sdf.parse("00:00:00");
                durationLong = durationD.getTime() - zeroSec.getTime();
            } catch (Exception e) {                     
                durationLong = (Long.parseLong(duration))/1000;                     
            }
        }
        else{
            String r = "";
            if(duration.contains("s")){
                   for (int i = 0; i < duration.length()-2; i ++) {
                          if ((duration.charAt(i) == '.')) 
                              break;
                          else
                              r += duration.charAt(i);
                   }
            }
            durationLong = Long.valueOf(r);
        }
        return durationLong;        
    }

If someone could find a better solution, please, tell me. Thanks everybody!

0

精彩评论

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

关注公众号