强制SimpleDateFormat解析整个字符串 [英] Force SimpleDateFormat to parse the whole string

查看:128
本文介绍了强制SimpleDateFormat解析整个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以下格式的日期:dd / MM / yyyy HH:mm:ss。我想从它创建一个 Date 对象,我正在使用 SimpleDateFormat 类。我们的应用程序接受存储在 String 数组中的许多不同的日期格式。我们做的是遍历该数组,使用 applyPattern 到我们的 SimpleDateFormat 对象,并尝试解析给定的日期。如果它抛出一个异常,我们在数组中尝试下一个日期格式等。

I have a datestring in the following format: "dd/MM/yyyy HH:mm:ss". I want to create a Date object from it and I'm using the SimpleDateFormat class. Our application accepts many different date formats that are stored in a String array. What we do is iterate through that array, use applyPattern to our SimpleDateFormat object and try to parse the given datestring. If it throws an exception we try the next date format in our array etc.

然而,我发现 parse code> SimpleDateFormat 类的方法不一定会尝试解析整个字符串。如果它从字符串的部分成功创建了一个Date对象,那么它将返回它。这里的问题是我们给定的datestring包含日期和时间数据,但是在我们的第一个解析中尝试使用 SimpleDateFormat 一个更简单的模式:dd / MM / yyyy。而且在解析过程中,它会在给定的日期中找到一个匹配的日期,它会在那里停止,并创建一个没有时间信息的 Date 对象。

However I found out that the parse method of the SimpleDateFormat class doesn't necessarily attempt to parse the whole string. If it successfully creates a Date object from parts of the string then it returns it. The problem here is that our given datestring contains both date and time data, however in our first parse attempt the SimpleDateFormat uses a simpler pattern: "dd/MM/yyyy". And since during the parsing process it finds a matching date in the given datestring it stops there and creates a Date object that has no time information.

有没有办法强制 SimpleDateFormat 来解析给出的整个字符串?

Is there a way to force SimpleDateFormat to parse the whole string it is given?

String dateString = "01/01/2015 05:30:00";
Date date = null;
for (String format : Constants.DATE_FORMATS) {//String Array that contains many date format strings.
    try {
        simpleDateFormat.applyPattern(format);//First format applied is "dd/MM/yyyy".
        date = simpleDateFormat.parse(dateString);
        //No exception thrown it accepts the "dd/MM/yyyy" part of the dateString even though the string itself contains even more data.
    }
    catch (Exception e) {}
}
//Returns a date object with the time set to 00:00:00


推荐答案

我不认为有办法强制 SimpleDateFormat 来解析整个String,但是可以将格式的长度与输入的长度进行比较。

I don't think there is a way to force the SimpleDateFormat to parse the whole String, but you can compare the length of the format with the length of the input

String[] formats = new String[] {"dd/MM/yyyy", "dd/MM/yyyy HH:mm:ss"};
String input = "25/09/2015 10:25:31";
Date result = null;

for (String format: formats) {
    if (input != null && format.length() == input.length()) {
        try {
            SimpleDateFormat sdFormat = new SimpleDateFormat(format);
            result = sdFormat.parse(input);
            break;
        }
        catch (ParseException e) {
        }
    }
}

编辑
另一种方法是使用 JodaTime 来解析String。

EDIT Another way is to use JodaTime to parse the String.

String[] formats = new String[] { "dd/MM/yyyy", "dd/MM/yyyy HH:mm:ss" };
String input = "25/09/2015 10:25:31";
LocalDateTime result = null;

for (String format : formats) {
    try {
        DateTimeFormatter sdFormat = DateTimeFormat.forPattern(format);
        result = sdFormat.parseLocalDateTime(input);
        break;
    }
    catch (IllegalArgumentException e) {
    }
}
System.out.println(result);

Date resultDate = result.toDate();
System.out.println(resultDate);

这给你

2015-09-25T10:25:31.000
Fri Sep 25 10:25:31 CEST 2015

这篇关于强制SimpleDateFormat解析整个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆