What's the nicest way to parse a date that can be in one of the following formats
"dd-MM-yyyy HH:mm"
"dd/MM/yyyy HH:mm"
"dd.MM.yyyy HH:mm"
without creating 3 SimpleDateFormats and parsing against each one.
Thanks
What's the nicest way to parse a date that can be in one of the following formats
"dd-MM-yyyy HH:mm"
"dd/MM/yyyy HH:mm"
"dd.MM.yyyy HH:mm"
without creating 3 SimpleDateFormats and parsing against each one.
Thanks
It's probably easiest to "tweak" the source string into a canonical format:
if (text.length() == 16)
{
if ((text.charAt(2) == '/' && text.charAt(5) == '/') ||
(text.charAt(2) == '.' && text.charAt(5) == '.'))
{
text = text.substring(0, 2) + "-" + text.substring(3, 5)
+ "-" + text.substring(6);
}
}
Then use the format string using "-".
Note that this is very specific, only replacing exactly the characters you're interested in, to avoid unwanted side-effects.
Could you run two replace operations first, so that you reduce all three formats to a single one?
It's probably easiest to "tweak" the source string into a canonical format:
if (text.length() == 16)
{
if ((text.charAt(2) == '/' && text.charAt(5) == '/') ||
(text.charAt(2) == '.' && text.charAt(5) == '.'))
{
text = text.substring(0, 2) + "-" + text.substring(3, 5)
+ "-" + text.substring(6);
}
}
Then use the format string using "-".
Note that this is very specific, only replacing exactly the characters you're interested in, to avoid unwanted side-effects.
You can do it using DateTimeFormatter
which allows you to specify optional things using square brackets.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.stream.Stream;
public class Main {
public static void main(String args[]) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d[-][.][/]M[-][.][/]u H:m", Locale.ENGLISH);
// Test
Stream.of(
"28-04-2021 14:01",
"28.04.2021 14:01",
"28/04/2021 14:01"
).forEach(s -> System.out.println(LocalDateTime.parse(s, dtf)));
}
}
Output:
2021-04-28T14:01
2021-04-28T14:01
2021-04-28T14:01
Learn more about the modern date-time API from Trail: Date Time.
Note that the legacy date-time API (java.util
date-time types and their formatting API, SimpleDateFormat
) are outdated and error-prone. It is recommended to stop using them completely and switch to java.time
, the modern date-time API* .
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
You may use Apache commons lang DateUtils.parseDate
import java.text.ParseException;
import org.apache.commons.lang.time.DateUtils;
public class Test {
public static void main(String[] args) throws ParseException {
String[] values = new String[]{"31-12-2009 12:00", "31/12/2009 12:00", "31.12.2009 12:00"};
String[] parsePatterns = new String[]{"dd-MM-yyyy HH:mm", "dd/MM/yyyy HH:mm", "dd.MM.yyyy HH:mm"};
for (String value : values) {
System.out.println(DateUtils.parseDate(value, parsePatterns));
}
}
}
Well, internally it creates SimpleDateFormats, but whats wrong with that?
how about a regex:
"\\d\\d[./-]\\d\\d[./-]\\d\\d\\d\\d \\d\\d:\\d\\d"
In code this would mean something like this:
Pattern pattern =
Pattern.compile("(\\d\\d)([./-])(\\d\\d)([./-])(\\d\\d\\d\\d) (\\d\\d):(\\d\\d)");
Matcher matcher =
pattern.matcher("31-07-1983 15:30");
if (matcher.find() && matcher.group(2).equals(matcher.group(4))) {
int day = Integer.parseInt(matcher.group(1));
int month = Integer.parseInt(matcher.group(3));
int year = Integer.parseInt(matcher.group(5));
int hour = Integer.parseInt(matcher.group(6));
int minute = Integer.parseInt(matcher.group(7));
}
Doing it yourself with a regular expression:
public class SpecialDateFormat
{
private final static Pattern PATTERN = Pattern.compile("(\\d{2})[\\.\\/\\-](\\d{2})[\\.\\/\\-](\\d{4}) (\\d{2}):(\\d{2})");
public static Date parse(String text) throws ParseException {
Matcher m = PATTERN.matcher(text);
if (m.matches()) {
int dd = Integer.parseInt(m.group(1));
int mm = Integer.parseInt(m.group(2));
int yy = Integer.parseInt(m.group(3));
int hh = Integer.parseInt(m.group(4));
int mi = Integer.parseInt(m.group(5));
// NOTE: Checking if values are in valid ranges omitted
Calendar cal = Calendar.getInstance();
cal.set(yy, mm - 1, dd, hh, mi, 0);
return cal.getTime();
}
else {
throw new ParseException("Unparseable date: " + text, 0);
}
}
}
Note however that this does allow mixing different separators, e.g. "17-09/2009 12:00" would be allowed.
ParseExact can take an array of formats. You still have to specify all formats, but it's a single operation.
© 2022 - 2024 — McMap. All rights reserved.