格式化日期和时间,仅当日期中包含时间时。

huangapple go评论93阅读模式
英文:

Formatting Date with time, only if there is a time on the date

问题

以下是修改后的代码:

import java.time.LocalDate;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;

public class StringUtil {
    public static String SanitiseDate(String input) {
        DateTimeFormatter[] possibleFormats = {
                DateTimeFormatter.ofPattern("uuuu-MM-dd").withResolverStyle(ResolverStyle.STRICT),
                DateTimeFormatter.ofPattern("uuuu/MM/dd").withResolverStyle(ResolverStyle.STRICT),
                DateTimeFormatter.ofPattern("dd/MM/uuuu").withResolverStyle(ResolverStyle.STRICT),
                DateTimeFormatter.ofPattern("dd-MM-uuuu").withResolverStyle(ResolverStyle.STRICT),
                DateTimeFormatter.ofPattern("MM-dd-uuuu").withResolverStyle(ResolverStyle.STRICT),

                DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss").withResolverStyle(ResolverStyle.STRICT),
                DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm").withResolverStyle(ResolverStyle.STRICT),

                DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm").withResolverStyle(ResolverStyle.STRICT),
                DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss").withResolverStyle(ResolverStyle.STRICT),

                DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss").withResolverStyle(ResolverStyle.STRICT),
                DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm").withResolverStyle(ResolverStyle.STRICT),

                DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm").withResolverStyle(ResolverStyle.STRICT),
                DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm:ss").withResolverStyle(ResolverStyle.STRICT),

                DateTimeFormatter.ofPattern("MM-dd-uuuu HH:mm").withResolverStyle(ResolverStyle.STRICT),
                DateTimeFormatter.ofPattern("MM-dd-uuuu HH:mm:ss").withResolverStyle(ResolverStyle.STRICT),
        };

        for (DateTimeFormatter format : possibleFormats) {
            try {
                LocalDate result = LocalDate.parse(input, format);
                if (result.getYear() <= 1900 || result.getYear() > Year.now().getValue()) {
                    return "";
                }
                if (format.equals(DateTimeFormatter.ofPattern("uuuu-MM-dd")) ||
                    format.equals(DateTimeFormatter.ofPattern("uuuu/MM/dd")) ||
                    format.equals(DateTimeFormatter.ofPattern("dd/MM/uuuu")) ||
                    format.equals(DateTimeFormatter.ofPattern("dd-MM-uuuu")) ||
                    format.equals(DateTimeFormatter.ofPattern("MM-dd-uuuu"))) {
                    return result.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                }
                return result.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            } catch (DateTimeParseException dtpe) {
                // ignore, try next format
            }
        }
        return "";
    }

    public static void main(String[] args) {
        System.out.println(SanitiseDate("01/01/2001"));
    }
}

这个修改后的代码对输入日期进行处理,根据指定的格式化规则将日期进行解析,并根据不同的情况返回不同格式的日期字符串。如果日期无效(小于等于1900年或大于当前年份),则返回空字符串。如果日期只包含日期部分而没有时间戳,则返回格式为yyyy-MM-dd的字符串;如果日期包含时间戳,则返回格式为yyyy-MM-dd HH:mm:ss的字符串。

英文:

I am trying to sanitize dates that are generated from another application. Sometimes the dates can be of a bad format, when they are, they should be blanked out. The general conditions should be;

  • Any dates on or before the year 1900 are considered invalid and blanked out

  • Any dates after the current year are considered invalid and blanked out

  • Any valid dates that match the patterns should returned in the output yyyy-MM-dd HH:mm:ss (where there is a timestamp available) OR yyyy-MM-dd when there is no timestamp available.

Dates <= 1900 and after the current year are considered to be invalid as they are being loaded into another system, where these dates won't make sense in the context of the application. For the same reason, I need to return the dates in the aforementioned format so they can be loaded into the next application.

How can I modify my code below to handle dates that do not have a timestamp? The current code gives an error of: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay when you try to call; SanitiseDate(&quot;01/01/2001&quot;)

Here is my code so far:

public static String SanitiseDate(String input) {
DateTimeFormatter[] possibleFormats = {
DateTimeFormatter.ofPattern(&quot;uuuu-MM-dd&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;uuuu/MM/dd&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;dd/MM/uuuu&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;dd-MM-uuuu&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;mm-DD-uuuu&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;uuuu-MM-dd HH:mm:ss&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;uuuu-MM-dd HH:mm&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;uuuu/MM/dd HH:mm&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;uuuu/MM/dd HH:mm:ss&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;dd/MM/uuuu HH:mm:ss&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;dd/MM/uuuu HH:mm&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;dd-MM-uuuu HH:mm&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;dd-MM-uuuu HH:mm:ss&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;mm-DD-uuuu HH:mm&quot;).withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern(&quot;mm-DD-uuuu HH:mm:ss&quot;).withResolverStyle(ResolverStyle.STRICT),
};
for (DateTimeFormatter format : possibleFormats) {
try {
LocalDate result = LocalDate.parse(input, format);
if (result.getYear() &lt;= 1900 || result.getYear() &gt; Year.now().getValue())
{
return &quot;&quot;;
}
return result.format(DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd HH:mm:ss&quot;));
} catch (DateTimeParseException dtpe) {
// ignore, try next format
}
}
return &quot;&quot;;
}

My tests:

@Test
public void sanitiseDateTest()
{
assertEquals(&quot;&quot;, StringUtil.SanitiseDate(&quot;&quot;));
assertEquals(&quot;&quot;, StringUtil.SanitiseDate(&quot;01/01/1601&quot;));
assertEquals(&quot;&quot;, StringUtil.SanitiseDate(&quot;01/01/1501&quot;));
assertEquals(&quot;2001-01-01&quot;, StringUtil.SanitiseDate(&quot;01/01/2001&quot;));
assertEquals(&quot;1980-01-01&quot;, StringUtil.SanitiseDate(&quot;01/01/1980&quot;));
assertEquals(&quot;&quot;, StringUtil.SanitiseDate(&quot;05/05/1900&quot;));
assertEquals(&quot;&quot;, StringUtil.SanitiseDate(&quot;01/01/1989&quot;));
assertEquals(&quot;1901-01-01&quot;, StringUtil.SanitiseDate(&quot;01/01/1901&quot;));
assertEquals(&quot;2020-02-29&quot;, StringUtil.SanitiseDate(&quot;29/02/2020&quot;));
assertEquals(&quot;2020-01-29&quot;, StringUtil.SanitiseDate(&quot;29/01/2020&quot;));
assertEquals(&quot;2020-01-29 01:00:00&quot;, StringUtil.SanitiseDate(&quot;29/01/2020 01:00:00&quot;));
assertEquals(&quot;&quot;, StringUtil.SanitiseDate(&quot;12/12/2030 01:00:00&quot;));
assertEquals(&quot;&quot;, StringUtil.SanitiseDate(&quot;12/12/2030 01:00:00; 12/12/2030 01:00:00;&quot;));
}

答案1

得分: 1

由于您想处理两种不同的输入并生成两种不同类型的输出,您需要在具有时间和不具有时间的输入日期之间拆分逻辑,并分别处理它们。我没有包括格式化程序,但我已将它们拆分为两个数组,一个用于没有时间模式的格式化程序,另一个用于具有时间模式的格式化程序。

if (input.length() < 10) {
    return "";
}

if (input.length() == 10) {
    for (final DateTimeFormatter format : possibleFormats1) {
        try {
            final LocalDate result = LocalDate.parse(input, format);
            if (result.getYear() <= 1900 || result.getYear() > Year.now().getValue()) {
                return "";
            }
            return result.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        } catch (final DateTimeParseException dtpe) {
            // ignore, try next format
        }
    }
} else {
    for (final DateTimeFormatter format : possibleFormats2) {
        try {
            final LocalDateTime result = LocalDateTime.parse(input, format);
            if (result.getYear() <= 1900 || result.getYear() > Year.now().getValue()) {
                return "";
            }
            return result.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        } catch (final DateTimeParseException dtpe) {
            // ignore, try next format
        }
    }
}
return "";
英文:

Since you want to handle two different inputs and also generate two kinds of output you need to split the logic between input dates with and without time and handle them separately. I didn't include the formatters but I have split them into two arrays, one for formatters without a time pattern and one for those with a time pattern

if (input.length() &lt; 10) { // maybe add check for &gt;
return &quot;&quot;; 
}
if (input.length() == 10) {
for (final DateTimeFormatter format : possibleFormats1) {
try {
final LocalDate result = LocalDate.parse(input, format);
if (result.getYear() &lt;= 1900 || result.getYear() &gt; Year.now().getValue()) {
return &quot;&quot;;
}
return result.format(DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd&quot;));
} catch (final DateTimeParseException dtpe) {
// ignore, try next format
}
}
} else {
for (final DateTimeFormatter format : possibleFormats2) {
try {
final LocalDateTime result = LocalDateTime.parse(input, format);
if (result.getYear() &lt;= 1900 || result.getYear() &gt; Year.now().getValue()) {
return &quot;&quot;;
}
return result.format(DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd HH:mm:ss&quot;));
} catch (final DateTimeParseException dtpe) {
// ignore, try next format
}
}
}
return &quot;&quot;;

huangapple
  • 本文由 发表于 2020年3月16日 19:51:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/60705480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定