简体   繁体   English

关于Java中的日期/时间格式

[英]Regarding format date/time in java

I have two datetime formats, 我有两种日期时间格式,

  1. 'dd-mm-yyyy' and 'dd-mm-yyyy'
  2. 'dd-mm-yyyy hh:MM:ss'

in my application. 在我的应用程序中。 The input value may be a string in any of above forms. 输入值可以是以上任何形式的字符串。

I want to store this date value in database. 我想将此日期值存储在数据库中。 So, first I format it with SimpleDateFormatter . 因此,首先我使用SimpleDateFormatter对其进行格式化。 But how can I check that input string is of type 1 or of type 2? 但是,如何检查输入字符串是1型还是2型?

If input date time type is in form of (2) and I format datetime with formatter 'dd-mm-yyyy' then it returns null . 如果输入日期时间类型为(2)的形式,并且我使用格式化程序'dd-mm-yyyy'格式化datetime,则返回null

Whatever input type - it must be converted according to input. 任何输入类型-必须根据输入进行转换。 In short, how can I check input datetime format? 简而言之,如何检查输入的日期时间格式?

i want to store this date value in data base. 我想将此日期值存储在数据库中。 So, first i formate it with SimpleDateFormatter , but how can i check that input string is type of 1 or type of 2. 因此,首先我使用SimpleDateFormatter对其进行格式化,但是如何检查输入字符串是1还是2。

It sounds like you want to parse the value - you shouldn't be storing it in the database as a string. 听起来好像您想解析该值-您不应该将它作为字符串存储在数据库中。 (Parsing is converting from a string to the natural data type, eg Date . Formatting is converting from a value to a string.) (解析是从字符串转换为自然数据类型,例如Date 。格式化是从值转换为字符串。)

You can just try parsing the value using DateFormat.parse , and catching the exception thrown if the text is invalid - but you should be aware that your format strings are wrong. 您可以尝试使用DateFormat.parse解析值,并在文本无效的情况下捕获抛出的异常-但您应注意格式字符串错误。 'M' is for month-of-year, and 'm' is for minutes, not the other way round. “ M”代表年份,“ m”代表分钟,而不是相反。 You also probably want the 24 hour clock (H), not the 12 hour clock (h), so you want: 您可能还想要24小时制(H),而不是12小时制(h),因此您需要:

dd-MM-yyyy

dd-MM-yyyy HH:mm:ss

Finally, I'd strongly advise you to use Joda Time instead of the built-in date/time classes. 最后,我强烈建议您使用Joda Time而不是内置的日期/时间类。 Joda Time is a much better API. 约达时间是一个更好的 API。 (For example, the formatters in Joda Time are thread-safe, unlike those in the JRE...) (例如,与JRE中的格式化程序不同,Joda Time中的格式化程序是线程安全的。)

Firstly, your data format is screwed up - you have minutes ( mm ) and months ( MM ) around the wrong way. 首先,您的数据格式搞砸了–错误的分钟数( mm )和月份( MM )。 ie your formats should be: 即您的格式应为:

"dd-MM-yyyy"
"dd-MM-yyyy HH:mm:ss"

Next, rather than being too smart about it, you can simply try each format and see what works: 接下来,您不必太过精打细算,只需尝试每种格式并查看有效的方法:

public static Date parseDate(String input) {
    String[] formats = { "dd-MM-yyyy", "dd-MM-yyyy HH:mm:ss" }; // Add more as you like

    for (String format : formats) {
        try {
            return new SimpleDateFormat(format).parse(input);
        }
        catch (ParseException e) {
            // Ignore and continue
        }
    }
    throw new IllegalArgumentException("Could not parse date from " + input);
}

There are many possibilities to distinguish the two formats. 区分这两种格式有很多可能性。 An easy one is to check the length of the input string. 一种简单的方法是检查输入字符串的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM